All times are UTC - 5 hours




Post new topic Reply to topic  [ 9 posts ] 
Author Message
 Post subject: About Question enthuware.ocajp.i.v7.2.1358 :
PostPosted: Sat Mar 01, 2014 2:13 am 
Offline

Joined: Sat Feb 15, 2014 12:38 pm
Posts: 8
Code:
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:
javac C:\Users\prashantj\TestClass.java


Top
 Profile Send private message  
 
 Post subject: Re: About Question enthuware.ocajp.i.v7.2.1358 :
PostPosted: Sat Mar 01, 2014 2:51 am 
Offline
Site Admin

Joined: Fri Sep 10, 2010 9:26 pm
Posts: 6802
You are not doing something right. The question and its answer are accurate.

thank you,
Paul.


Top
 Profile Send private message  
 
 Post subject: Re: About Question enthuware.ocajp.i.v7.2.1358 :
PostPosted: Wed Nov 26, 2014 1:38 am 
Offline

Joined: Fri Oct 31, 2014 6:31 pm
Posts: 63
What is it mean by assertions are disabled by default?


Top
 Profile Send private message  
 
 Post subject: Re: About Question enthuware.ocajp.i.v7.2.1358 :
PostPosted: Wed Nov 26, 2014 2:40 am 
Offline
Site Admin

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


Top
 Profile Send private message  
 
 Post subject: Re: About Question enthuware.ocajp.i.v7.2.1358 :
PostPosted: Tue Mar 28, 2017 11:12 pm 
Offline

Joined: Sat Mar 25, 2017 5:38 am
Posts: 19
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??


Top
 Profile Send private message  
 
 Post subject: Re: About Question enthuware.ocajp.i.v7.2.1358 :
PostPosted: Tue Mar 28, 2017 11:44 pm 
Offline
Site Admin

Joined: Fri Sep 10, 2010 9:26 pm
Posts: 6802
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.


Top
 Profile Send private message  
 
 Post subject: Re: About Question enthuware.ocajp.i.v7.2.1358 :
PostPosted: Wed Sep 13, 2017 4:06 pm 
Offline

Joined: Sat Mar 25, 2017 5:38 am
Posts: 19
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();
}
}


Top
 Profile Send private message  
 
 Post subject: Re: About Question enthuware.ocajp.i.v7.2.1358 :
PostPosted: Wed Sep 13, 2017 8:07 pm 
Offline

Joined: Sat Mar 25, 2017 5:38 am
Posts: 19
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. ;)


Top
 Profile Send private message  
 
 Post subject: Re: About Question enthuware.ocajp.i.v7.2.1358 :
PostPosted: Wed Sep 13, 2017 8:26 pm 
Offline
Site Admin

Joined: Fri Sep 10, 2010 9:26 pm
Posts: 6802
Correct.


Top
 Profile Send private message  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 9 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 8 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
Template made by DEVPPL/ThatBigForum