• Post Reply Bookmark Topic Watch Topic
  • New Topic

Why we throw exception ?  RSS feed

 
Ranch Hand
Posts: 397
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,

Why we throw exception as we have already try/catch block to handle the exceptional condition.

I think only benefit is to throw user defined exception . correct ?
 
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you give an example SSCCE that demonstrates what you're talking about?
 
Ranch Hand
Posts: 58
Eclipse IDE Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In many cases you would not want to handle the exception by using a try/catch block. It might make more sense to pass on the exception to the caller.
For instance, say your method encounters an Exception while reading information from a database. You could then throw this exception back to the caller of your method who can then choose to handle it any way he likes. For e.g display an error message to the user, log it in a file etc.
 
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can also have incorrect input, in which case the Exception is used to prevent that incorrect input causing more problems later on.
 
Prabhat Ranjan
Ranch Hand
Posts: 397
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
could anyone help with some examples
 
Campbell Ritchie
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you read the Java™ Tutorials section about exceptions?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!