• Post Reply Bookmark Topic Watch Topic
  • New Topic

Why is not better to catch Runtime Exceptions?  RSS feed

 
Ravi Kiran Va
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,

Please tell me Why is not better to catch Runtime Exceptions ??

"Typically, the cost of checking for runtime exceptions exceeds the benefit of catching or specifying them. Thus the compiler does not require that you catch or specify runtime exceptions, although you can. "

This is mentioned in API

could anybody please explain.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Runtime exceptions are used to signal that something unexpected happened in the program, which usually means that there is a bug in the program. It doesn't really make sense to force the developer to deal with runtime exceptions while (s)he is writing code; it would for example be very cumbersome if the compiler would require you to deal with a possible NullPointerException every time you access values in an object through a variable. If NullPointerException would be a checked exception, then practically all methods that you'd write have to have "throws NullPointerException" declared.

The idea is that when a runtime exception happens in your code, there's nothing that the program itself could reasonably do to correct the error; the developer needs to find out what's wrong and fix the bug.

This page from The Java Tutorials explains it in more detail: The Catch or Specify Requirement.
 
Ravi Kiran Va
Ranch Hand
Posts: 2234
Eclipse IDE Firefox Browser Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
got it , thanks
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!