File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes How to recognise a checked or unchecked exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to recognise a checked or unchecked exception" Watch "How to recognise a checked or unchecked exception" New topic
Author

How to recognise a checked or unchecked exception

Arindam Ghosh
Greenhorn

Joined: Feb 21, 2007
Posts: 1
We know that checked exceptions are runtime exception and unchecked exceptions are non-runtime exceptions but how to recognize that it is a checked exception or an unchecked exception.
vijay kumarg
Ranch Hand

Joined: Dec 14, 2006
Posts: 105
Hi Arindam,
I think your defintion does not differntiate checked and unchecked exceptions.(Subject to correction)

Checked exceptions are the type of exceptions you must declare in throws clause or put a try catch block to handle it, otherwise there is a violation and compiler would not compile the program.egs are IOException
But this does not applies to unchecked exceptions where it is not compulsary to declare or handle them eg is NullPointerException.

Thanks,
Sidd Kulk
Ranch Hand

Joined: Feb 20, 2007
Posts: 152
The exception classes which extend Error or RuntimeException are unchecked exceptions. The Java compiler doesn't force us to tackle these exceptions. For example,

class ExceptionTest{
public static void main(String[] sid){
throw new Exception();
}
}
//The above code won't compile, as we don't declare the exception in the method definition.

// But the following code would compile

class ExceptionTest{
public static void main(String[] sid){
throw new Error();
// or throw new RuntimeException();
}
}
// e.g. of checked exceptions-IOException etc.
// e.g. of unchecked exceptions-ClassCastExeption etc.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39865
    
  28
Welcome to the Ranch. Vijay Kumarg is right about checked exception being those where the compiler enforces "catch" or "throws". Look in the Java tutorial about exceptions, where it tells you that and why unchecked Exceptions are those which extend java.lang.RuntimeException, and checked Exceptions those which extend java.lang.Exception and not RuntimeException.
Go into the API specifications and look for the inheritance of a few Exceptions, eg ArrayIndexOutOfBoundsException, IOException, NullPointerException, and FileNotFoundException, and see which do and don't extend RuntimeException.

CR.
[ February 21, 2007: Message edited by: Campbell Ritchie ]
 
Consider Paul's rocket mass heater.
 
subject: How to recognise a checked or unchecked exception