• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

checked and unchecked exceptions

 
rachana rayaguda
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i was told that 'throw' and 'throws' throw objects of unchecked exceptions. and that they can also throw checked exceptions but the compiler ignores them.
my question: why does the compiler ignore them?
[ November 20, 2007: Message edited by: rachana rayaguda ]
 
R van Vliet
Ranch Hand
Posts: 144
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note sure I completely understand your question, but exceptions become checked if your method signature mentions the exception in it's throws clause.

Once you define your method to throw a specific exception, the compiler will complain if you dont catch that exception where that method is called.
 
Rob Spoor
Sheriff
Pie
Posts: 20749
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by R van Vliet:
... exceptions become checked if your method signature mentions the exception in it's throws clause.

That's nonsense.

An exception is checked if it's Exception or a subclass but not RuntimeException or a subclass. If the latter is true then it is an unchecked exception. Declaring it has nothing to do with it; if you declare that a method throws a NullPointerException that does not make the NullPointerException a checked exception.

It is true however that a checked exception must either be caught, or declared to be thrown.
 
Bill Cruise
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob is correct. An exception is checked if it does not inherit from RuntimeException. I always think of checked as meaning "checked by the compiler", because the compiler tells me when I forget to surround checked exceptions with a try-catch block. RuntimeExceptions can occur on literally any line of code, so it would be tedious to require try-catch blocks for them.

I recommend the Java Tutorial on Exceptions to anyone who hasn't read through it yet. http://java.sun.com/docs/books/tutorial/essential/exceptions/
 
Raghavan Muthu
Ranch Hand
Posts: 3381
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The word 'checked' means the exceptions you expect to be thrown should be checked by the compiler at the compilation time.

The word 'unchecked' is on the other way.

Technically, the checked exceptions are of subtypes of java.lang.Exception and unchecked exceptions are of type java.lang.RuntimeException.

Hope this helps on a high level.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic