• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

catching run time exception?

 
ankur rathi
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why we should not catch java.lang.RunTimeException or it's sub classes???

Thanks a lot.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15216
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Scott Selikoff
author
Saloon Keeper
Posts: 4010
18
Eclipse IDE Flex Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
ankur rathi
Ranch Hand
Posts: 3830
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15216
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic