• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

K&B Exception Handling Question

 
thomas jacob
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Jigo
 
Marcos Vilela
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

thanks
[ July 15, 2005: Message edited by: Marcos Vilela ]
 
Devender Thareja
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic