my dog learned polymorphism
The moose likes Beginning Java and the fly likes checked and unchecked exceptions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "checked and unchecked exceptions" Watch "checked and unchecked exceptions" New topic

checked and unchecked exceptions

rachana rayaguda

Joined: Aug 05, 2007
Posts: 16
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

Joined: Nov 10, 2007
Posts: 144
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

Joined: Oct 27, 2005
Posts: 20279

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.

How To Ask Questions How To Answer Questions
Bill Cruise
Ranch Hand

Joined: Jun 01, 2007
Posts: 148
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.
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3381

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.

Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
I agree. Here's the link:
subject: checked and unchecked exceptions
It's not a secret anymore!