This week's book giveaway is in the Web Services forum.
We're giving away four copies of Microservices in Action and have Morgan Bruce & Paulo A. Pereira on-line!
See this thread for details.
Win a copy of Microservices in Action this week in the Web Services forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

How to recognise a checked or unchecked exception  RSS feed

 
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.
 
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,
 
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.
 
Marshal
Posts: 61788
193
  • 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 ]
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!