aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes wizlab mock question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "wizlab mock question" Watch "wizlab mock question" New topic
Author

wizlab mock question

Shashank Sharma
Ranch Hand

Joined: Sep 27, 2006
Posts: 91




this code should right away give error when throwing ex in inner catch block exception not handeled.
how does above code prints catchfinally1finally2 .it goes fine when i un comment line 1
Milan Sutaria
Ranch Hand

Joined: Jul 10, 2008
Posts: 122
i find no problem. can you explain further why should it give error?


SCJP 6 83%
Working on SCWCD/OCPJWCD
Tom Gustafsson
Greenhorn

Joined: Aug 10, 2008
Posts: 6
"
this code should right away give error when throwing ex in inner catch block exception not handeled.
how does above code prints catchfinally1finally2 .it goes fine when i un comment line 1
"

I am not sure whether I understood the question, but it goes to finally block before throwing the exception. If you have the exit uncommented, it won't throw exception because it exits execution before throwing.

try {
throw new RuntimeException();
}finally {
System.out.println("Finally");
}

That one also prints finally before throwing exception.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9305
    
  17



First of all the code will not give any compilation error as you have declared Exception in the throws clause of the main method.

Now lets go through the execution of the program...

The exception thrown at (1) is caught at (2).

another exception is thrown at (3) which is again caught at (4)

Line (5) is executed and "Catch" is displayed.

Another exception is thrown at (6) which is not caught.

As you know finally is always executed whether try or any catch trows any exception or run successfully or return any value.
So the finally associated with try(2) is executed. Here Finally1 is displayed at (7) so total "CatchFinally1" has been displayed till now.

Now the finally associated with try(1) is executed which displays Finally2 at (8).
So "CatchFinally1Finally2" is displayed at the end.

Now if you comment line (9) then Exception is thrown.
If you uncomment line (9) then the program exits without throwing any exception.


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Shashank Sharma
Ranch Hand

Joined: Sep 27, 2006
Posts: 91
thanks
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: wizlab mock question