This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Runtime Exception

 
jeff rusty
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why Runtime Exceptions are Not Checked in java..
 
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
Java has two kinds of exceptions: checked and unchecked exceptions. According to the specification, java.lang.RuntimeException and all other exceptions that are subclasses of RuntimeException are unchecked; all other exceptions are checked.

That's just how it is defined and there's no asking "why".

If your question is really: "Why do we have a distinction between checked and unchecked exceptions in Java?": In software, there are generally two kinds of errors that can happen while a program is running: expected errors and unexpected errors.

An expected error is something that the programmer can expect to go wrong sometimes. An example of this is a network connection error. The computer might not be connected to the network when the program runs, and the program should handle such an error nicely, for example by showing an error message to the user.

An unexpected error happens when something goes wrong in the program unexpectedly, and it is almost always an indication that there's a bug in the program. As the programmer, you can't do much about that while the program is running - all you can do is debug the program afterwards and fix the error in your code.

Java checked and unchecked exceptions roughly compare to expected and unexpected errors. You are supposed to handle checked exceptions, and that's why the compiler checks if you handle them properly.

When you write your own exception classes, you should think about whether your exception represents an expected error or an unexpected error and subclass Exception or RuntimeException accordingly.
 
Robert Hill
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Becasue some exceptions are not known until the program is running.

Besides if all exceptions were checked at compile time, most of your code would have to be exception handling code.
 
jeff rusty
Ranch Hand
Posts: 109
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks
 
Prem Kashyap
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Checked exception needs to be thrown or caught if you want your program to compile.
Although you also have the liberty to catch or throw unchecked exception like NullPointer Exception, but it would be a very bad programming practise and it is highly discouraged. Generally we use assert to test our application for unchecked exception.

Regards
Prem
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic