This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes catching run time exception? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "catching run time exception?" Watch "catching run time exception?" New topic
Author

catching run time exception?

ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
Why we should not catch java.lang.RunTimeException or it's sub classes???

Thanks a lot.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13869
    
  10

Because a runtime (unchecked) exception usually means that there's a bug in your program.

Usually you don't want your program to continue running if an unexpected exception happens, because it means that your program is in an undefined state, and it doesn't make much sense to keep running if the program is in an undefined state.

Some people follow a different philosophy, for example in the Spring framework, unchecked exceptions are used almost exclusively.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Scott Selikoff
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3697
    
    5

In a professional system though, you want to catch all exceptions on the highest level so as to present a clean error message saying something failed, often a message saying "Error, Please contact an Administrator". It doesn't do much good if customers using your system recieve a large stack trace.


My Blog: Down Home Country Coding with Scott Selikoff
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
Originally posted by Jesper de Jong:
Because a runtime (unchecked) exception usually means that there's a bug in your program.

Usually you don't want your program to continue running if an unexpected exception happens, because it means that your program is in an undefined state, and it doesn't make much sense to keep running if the program is in an undefined state.

Some people follow a different philosophy, for example in the Spring framework, unchecked exceptions are used almost exclusively.


I agree, that they come due to programming fault. But we can't show exceptions to user???

What you say???

Thanks.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13869
    
  10

Originally posted by rathi ji:
I agree, that they come due to programming fault. But we can't show exceptions to user???

What you say???

Thanks.


Ofcourse you can and you should, see Scott's answer.
But ofcourse your software should not contain bugs that cause runtime exceptions in the first place...
 
 
subject: catching run time exception?
 
Similar Threads
about object serialization
JSP init parameters.
WA #1.....word association
5 letter scrabble
JavaRanch Olympics