This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes K&B Exception Handling Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "K&B Exception Handling Question" Watch "K&B Exception Handling Question" New topic

K&B Exception Handling Question

thomas jacob
Ranch Hand

Joined: May 19, 2005
Posts: 91
1.public class RTExcept
2. public static void throwit(){
3. System.out.print("throwit ");
4. throw new RuntimeException();
5. }
6. public static void main(String[] args){
7. try{
8. System.out.print("hello");
9. throwit();
10. }
11. catch(Exception re ) {
12. System.out.print("caught");
13. }
14. finally {
15. System.out.print("finally");
16. }
17. System.out.println("after");
18. }
19. }

The answer in K&B is hello throwit caught finally after

My doubt is how come "after" is printed, when an exception occurs doesn't the code exits the VM after the finally or is it if the exception is caught it executes the code to the bottom of the method stack call.

Thanks in advance
Marcos Vilela

Joined: Jul 06, 2005
Posts: 18
That's why we use a caught. To keep the code running.. It doesn't make sense stop after caught the exception.

I don't know if it's right..
correct me if I'm wrong.

[ July 15, 2005: Message edited by: Marcos Vilela ]
Devender Thareja
Ranch Hand

Joined: Jul 14, 2005
Posts: 187
That's right. If the exception is caught, program will follow instructions in exception block.
If there is no catch exception block, the exception will be thrown back to JVM and program will be aborted with error messages.

Devender Thareja
It is sorta covered in the JavaRanch Style Guide.
subject: K&B Exception Handling Question
Similar Threads
throwing exceptions
Exceptions question
unreachable code
Question regarding execution order when exception occurs
K&B uncaught exception behaviour