• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

How to recognise a checked or unchecked exception

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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,
 
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 79956
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
sunglasses are a type of coolness prosthetic. Check out the sunglasses on this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic