Home arrow static arrow Java Programming [Archive] - Checked and Unchecked Exception
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - Checked and Unchecked Exception
This topic has 2 replies on 1 page.

Posts:37
Registered: 5/11/04
Checked and Unchecked Exception  
Jun 14, 2004 3:15 PM



 
I cant get whats meant by checked and unchecked Exception, Can anyone suggest me with better explanation. Example is quite appreciable...

Regards

Shan
java2ee@linux.net
 

Posts:6,750
Registered: 1/25/04
Re: Checked and Unchecked Exception  
Jun 14, 2004 3:21 PM (reply 1 of 2)



 
See what happens when you try to compile this.
public class Example {public void aMethod() {  doSomething();  doSomethingDangerous();} private void doSomething() {  throw new IllegalArgumentException("I'm in a bad mood");} private void doSomethingDangerous() throws Exception {   System.out.println("I guess it's safe after all");}}
 

Posts:37,103
Registered: 3/30/99
Re: Checked and Unchecked Exception  
Jun 14, 2004 3:24 PM (reply 2 of 2)



 
Checked exceptions represent something that both a) is a result of external influences beyond your control, not (necessarily) bad code and b) is reasonable to try to recover from (generally because it's at the application level, rather than a problem with the VM, OS, etc.).

For each checked exception that can be thrown while executing the body of a method, that method must either a) catch the exception (or one of its ancestors) or b) declare in its throws clause that it throws that exception (or one of its ancestors). This means a caller of your method knows about everything that can go wrong that it might reasonably be expected to deal with.

Unchecked exceptions either a) are a result of buggy code (RuntimeException and its descendants) or b) are a result of a problem with the VM or OS, etc. (Error and its descendants). If a, then you should fix your code, not try to handle the exception. If b, then there's probably not much you can do, so there's not usually much point it trying to handle it. Unchecked exceptions can be thrown anywhere without being caught or declared.

http://java.sun.com/docs/books/tutorial/essential/exceptions/index.html

 
This topic has 2 replies on 1 page.