• Post Reply Bookmark Topic Watch Topic
  • New Topic

Handling the unchecked Exception  RSS feed

 
Vivek Kr Agrawal
Ranch Hand
Posts: 51
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
Sorry to discuss this topic when there are literally thousands of links there. But sadly I found not of them giving me the exact solution.

My knowledge about unchecked Exception is "Any exception that can not be handled."
According to Oracle "Runtime exceptions are not subject to the Catch or Specify Requirement." And not to forget that Runtime Exceptions are just an unchecked Exception.

Consider the scenario
Now this


In the second scenario I have just successfully handled the unchecked exception. So why it is said that
Runtime exceptions are not subject to the Catch or Specify Requirement.
. Can someone please explain me what going on here?
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vivek Kr Agrawal wrote:In the second scenario I have just successfully handled the unchecked exception. So why it is said that
Runtime exceptions are not subject to the Catch or Specify Requirement.
. Can someone please explain me what going on here?

All it's saying is that you don't have to use try...catch or throws with an unchecked exception; but there's absolutely nothing to stop you doing so if you want to - as you've plainly proved.

FYI - Your example should catch ArithmeticException, not Exception.
1. It's just a general rule: make your catch statements as explicit as possible.
2. The code in your catch block is specifically for dealing with an ArithmeticException, so you probably don't want to execute it if any other Exception is thrown.

HIH

Winston
 
manish ghildiyal
Ranch Hand
Posts: 136
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Vivek, what it actually means is that compiler won't force you to handle unchecked exception unlike
checked exceptions which you need to handle. Unchecked exceptions are meant to determine
bad code scenarios like divison by zero or calling method on null reference, so its better if they are
caught during testing. Checked exceptions are meant to handle such scenarios where you won't
want your program to die just because something happened which is not in your control, like user
giving wrong file name.

Manish
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!