GeeCON Prague 2014*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Exception handling problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Exception handling problem" Watch "Exception handling problem" New topic
Author

Exception handling problem

Mohit G Gupta
Ranch Hand

Joined: May 18, 2010
Posts: 634



1.if i uncomment the commented line,i get
unreachable statement
System.out.println("me e eemeemme");

2.while if i uncomment
System.out.println("me e eemeemme");
and comment throw new RuntimeException();
then,i don't get this compiletimeerror.

when i try to put as in array aas nullpointerexception would be thrown,so exception is passed from call to main and exception occurs at runtime
why isn't there a compiletime error ???
as in
unreachable statement
System.out.println("me e eemeemme");


3.when i remove
String [] aas=null;
aas[0]="as";
and uncomment
throw new RuntimeException();

System.out.println("me e eemeemme");
then,i get the
unreachable statement
System.out.println("me e eemeemme");


why such unsual behaviour when JVM itself throws Exception and when thrown by user


OCPJP 6.0 93%
OCPJWCD 5.0 98%
Prasad Kharkar
Ranch Hand

Joined: Mar 07, 2010
Posts: 446
    
    1

mohitkumar gupta wrote:

1.if i uncomment the commented line,i get
unreachable statement
System.out.println("me e eemeemme");

2.while if i uncomment
System.out.println("me e eemeemme");
and comment throw new RuntimeException();
then,i don't get this compiletimeerror.

When we are writing the throw keyword, then the compiler knows for sure that we are throwing the exception and hence, the statements after throwing the exception will not be able to reach at all so the error is unreachable statement
but when you comment out the throw new RuntimeException then there is no throwing of exception by user and hence there is no exception also and hence, the compiler can go further for compilation and hence there is no error

mohitkumar gupta wrote:
exception is passed from call to main and exception occurs at runtime
why isn't there a compiletime error ???
as in
unreachable statement
System.out.println("me e eemeemme");



all the method calls are at runtime and hence even if there is possibility that exception will be thrown at runtime, compiler cannot know that
hence it compiles fine
mohitkumar gupta wrote:
3.when i remove
String [] aas=null;
aas[0]="as";
and uncomment
throw new RuntimeException();

System.out.println("me e eemeemme");
then,i get the
unreachable statement
System.out.println("me e eemeemme");


why such unsual behaviour when JVM itself throws Exception and when thrown by user


answer to the first question definitely solves this also that is because of the throw keyword

hope this helps
happy preparation Mohitkumar

SCJP 6 [86%], OCPWCD [84%], OCEJPAD [83%]
If you find any post useful, click the "plus one" sign on the right
Arjun Srivastava
Ranch Hand

Joined: Jun 23, 2010
Posts: 432

what is the source of this question?


SCJP 6 | FB : Java Certifications-Help. | India Against Corruption
 
GeeCON Prague 2014
 
subject: Exception handling problem