About Question enthuware.ocajp.i.v7.2.1358 :

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

Moderator: admin

Post Reply
prashantjain25
Posts: 9
Joined: Sat Feb 15, 2014 12:38 pm
Contact:

About Question enthuware.ocajp.i.v7.2.1358 :

Post by prashantjain25 »

Code: Select all

package sample;

public class TestClass {
    public static void methodX() throws Exception {
        throw new AssertionError();}

    public static void main(String[] args) {
        try{
            methodX(); 
            }
        catch(Exception e) {
            System.out.println("EXCEPTION");
            }
        }
}
It does not throw AssertionError instead Error: Could not find or load main class C:\Users\prashantj\TestClass.class for which compilation was successful

Code: Select all

javac C:\Users\prashantj\TestClass.java

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

Re: About Question enthuware.ocajp.i.v7.2.1358 :

Post by admin »

You are not doing something right. The question and its answer are accurate.

thank you,
Paul.
If you like our products and services, please help us by posting your review here.

gparLondon
Posts: 63
Joined: Fri Oct 31, 2014 6:31 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1358 :

Post by gparLondon »

What is it mean by assertions are disabled by default?

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

Re: About Question enthuware.ocajp.i.v7.2.1358 :

Post by admin »

It means that they aren't executed at run time if you don't explicitly enable them on the command line using the -ea switch. You may want to go through this: https://docs.oracle.com/javase/jp/8/tec ... le-disable
If you like our products and services, please help us by posting your review here.

mjmsausava
Posts: 19
Joined: Sat Mar 25, 2017 5:38 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1358 :

Post by mjmsausava »

Even though the methodX() call is in try block, should the main() method not declare throws in its declaration as well because methodX() declares throws??

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

Re: About Question enthuware.ocajp.i.v7.2.1358 :

Post by admin »

The call to methodX in main is already wrapped inside a try/catch. If an exception is thrown by methodX, it will be caught by this try/catch block and be consumed inside the main method itself. It will never be propagated outside the main method. Therefore there is no need to declare Exception in main method's throws clause.

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

mjmsausava
Posts: 19
Joined: Sat Mar 25, 2017 5:38 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1358 :

Post by mjmsausava »

admin wrote:The call to methodX in main is already wrapped inside a try/catch. If an exception is thrown by methodX, it will be caught by this try/catch block and be consumed inside the main method itself. It will never be propagated outside the main method. Therefore there is no need to declare Exception in main method's throws clause.
Then why the following code (QID - 2.1301) will not compile without the main method declaring:

class SomeThrowable extends Throwable { }

class MyThrowable extends SomeThrowable { }

public class TestClass{

public static void main(String args[]) throws SomeThrowable{
try{
m1();
}catch(SomeThrowable e){
throw e;
}finally{
System.out.println("Done");
}
}
public static void m1() throws MyThrowable{
throw new MyThrowable();
}
}

mjmsausava
Posts: 19
Joined: Sat Mar 25, 2017 5:38 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1358 :

Post by mjmsausava »

oh. Never mind. I think I got the answer. The declare in main method is due to exception being thrown in catch block. Hope I am right. ;)

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

Re: About Question enthuware.ocajp.i.v7.2.1358 :

Post by admin »

Correct.
If you like our products and services, please help us by posting your review here.

elias86
Posts: 7
Joined: Fri May 04, 2018 4:14 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1358 :

Post by elias86 »

It will throw AssertionError out of the main method means that that AssertionError is thrown by methodX() and, being not catched in methodX(), and (being that not a valid exception), after the thrown of AssertionError the program stops executing without continue in main() method?

Thanks, Elias.

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

Re: About Question enthuware.ocajp.i.v7.2.1358 :

Post by admin »

Correct. The call to methodX(); in main throws an AssertionError, which cannot be caught by catch(Exception ). So the main method cannot continue after the call to methodX();
If you like our products and services, please help us by posting your review here.

st.lisker
Posts: 22
Joined: Sat Jun 30, 2018 6:11 am
Contact:

Re: About Question enthuware.ocajp.i.v7.2.1358 :

Post by st.lisker »

Hello. The correct answer is :
"It will throw AssertionError ."
What is it mean " out of the main method "?

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

Re: About Question enthuware.ocajp.i.v7.2.1358 :

Post by admin »

"out of the method" just means that whoever calls the method will receive the exception. To make it clear that the exception generated inside the method body isn't handled inside the method itself and is therefore not thrown outside the method.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest