• Post Reply Bookmark Topic Watch Topic
  • New Topic

Exceptions  RSS feed

 
Ransika deSilva
Ranch Hand
Posts: 524
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have 2 questions.
If an uncaught exception occurs, what happens to the program. I read that, it searches for another try block which catches the thrown error.
In a try-catch block if the catch block also throws an exception, what happens to the program?.
 
Sadanand Murthy
Ranch Hand
Posts: 382
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by ransika desilva:
I have 2 questions.
If an uncaught exception occurs, what happens to the program. I read that, it searches for another try block which catches the thrown error.

The best way to find out what happens is to write a program that does this.
If an uncaught exception occurs & if the calling method's signature does not have a throws statement or does not throw the uncaught exception or any of its super then the program will not process anything further; if it is an application, it will terminate.
Originally posted by ransika desilva:
In a try-catch block if the catch block also throws an exception, what happens to the program?.

Depends. If the calling method has a catch block for this exception or its super then the control will flow there. If the calling method, instead, has 'throws' statement for this this exception or its super then the control will flow to the calling method's caller & thus up the line till it either finds a matching catch block or terminates if a matching catch block is not found.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you throw something in the try block, it can be caught in the associated catch block - if you catch something compatible with the thrown exception. If you throw something in the catch block, the exception will bubble up to the next appropriate catch block, either in the same method or not. Say MyException A & B both extend Exception:

The MyExceptionA is not caught, and will start seeking a catch that does match.

This time we catch one exception and throw another, kinda like converting the exception from one type to another, but we also lost the original stack trace.
You might want to download Thinking In Java. As I recall Bruce Eckel has a pretty good chapter on exceptions. Hope that helps!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!