• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to recognise a checked or unchecked exception

 
Arindam Ghosh
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 152
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48381
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic