Page 1 of 1

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

Posted: Sat Mar 01, 2014 2:13 am
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

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

Posted: Sat Mar 01, 2014 2:51 am
by admin
You are not doing something right. The question and its answer are accurate.

thank you,
Paul.

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

Posted: Wed Nov 26, 2014 1:38 am
by gparLondon
What is it mean by assertions are disabled by default?

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

Posted: Wed Nov 26, 2014 2:40 am
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

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

Posted: Tue Mar 28, 2017 11:12 pm
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??

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

Posted: Tue Mar 28, 2017 11:44 pm
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.

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

Posted: Wed Sep 13, 2017 4:06 pm
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();
}
}

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

Posted: Wed Sep 13, 2017 8:07 pm
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. ;)

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

Posted: Wed Sep 13, 2017 8:26 pm
by admin
Correct.

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

Posted: Sat May 05, 2018 8:50 am
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.

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

Posted: Sat May 05, 2018 9:05 am
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();

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

Posted: Sun Jul 01, 2018 4:04 am
by st.lisker
Hello. The correct answer is :
"It will throw AssertionError ."
What is it mean " out of the main method "?

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

Posted: Sun Jul 01, 2018 4:38 am
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.