[HD-OCP17/21-Fundamentals Pg 166, Sec. 6.4.4 - instance-initializers-vs-constructors]

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
raphaelzintec
Posts: 167
Joined: Sun Apr 21, 2024 10:43 am
Contact:

[HD-OCP17/21-Fundamentals Pg 166, Sec. 6.4.4 - instance-initializers-vs-constructors]

Post by raphaelzintec »

hello

why is this instance initializer correct ?? and compile fine??

Code: Select all

    {
        try {
            throw new Exception();
        } catch (RuntimeException e){

        }
    }
because if i put this code inside main then compiler will complain and its logic because subtype cannot catch superclass!!
Last edited by raphaelzintec on Thu Sep 19, 2024 11:27 pm, edited 1 time in total.

raphaelzintec
Posts: 167
Joined: Sun Apr 21, 2024 10:43 am
Contact:

Re: [HD-OCP17/21-Fundamentals Pg 166, Sec. 6.4.4 - instance-initializers-vs-constructors]

Post by raphaelzintec »

also why is this giving error

Code: Select all

    {
        throw new Exception();
    }

    constructor() throws Exception{
    }
but this compiles fine???

Code: Select all

    {
       if(true) throw new Exception();
    }

    constructor() throws Exception{
    }

admin
Site Admin
Posts: 10385
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: [HD-OCP17/21-Fundamentals Pg 166, Sec. 6.4.4 - instance-initializers-vs-constructors]

Post by admin »

This is explained in the chapter 1. section 1.2.1

Post Reply

Who is online

Users browsing this forum: No registered users and 19 guests