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

Moderator: admin

prashantjain25
Posts: 8
Joined: Sat Feb 15, 2014 12:38 pm
Contact:

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

Post by prashantjain25 » Sat Mar 01, 2014 2:13 am

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: 7186
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin » Sat Mar 01, 2014 2:51 am

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

thank you,
Paul.

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

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

Post by gparLondon » Wed Nov 26, 2014 1:38 am

What is it mean by assertions are disabled by default?

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

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

Post by admin » Wed Nov 26, 2014 2:40 am

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

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

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

Post by mjmsausava » Tue Mar 28, 2017 11:12 pm

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: 7186
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin » Tue Mar 28, 2017 11:44 pm

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.

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

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

Post by mjmsausava » Wed Sep 13, 2017 4:06 pm

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 » Wed Sep 13, 2017 8:07 pm

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: 7186
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin » Wed Sep 13, 2017 8:26 pm

Correct.

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

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

Post by elias86 » Sat May 05, 2018 8:50 am

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.

Post Reply

Who is online

Users browsing this forum: No registered users and 7 guests