wood burning stoves 2.0*
The moose likes Java in General and the fly likes Runtime Exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Runtime Exception" Watch "Runtime Exception" New topic
Author

Runtime Exception

jeff rusty
Ranch Hand

Joined: Nov 07, 2006
Posts: 109
Why Runtime Exceptions are Not Checked in java..
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14269
    
  21

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Robert Hill
Ranch Hand

Joined: Feb 24, 2006
Posts: 94
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

Joined: Nov 07, 2006
Posts: 109
thanks
Prem Kashyap
Ranch Hand

Joined: Oct 10, 2006
Posts: 52
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Runtime Exception