A question about exceptions

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
Jix
Posts: 14
Joined: Thu May 31, 2012 7:57 pm
Contact:

A question about exceptions

Post by Jix »

Sorry for the long post But I'm really confused with Exceptions, I noticed that sometimes when an exception occurs in a method, the method has to declare that it throws this exception. And sometimes it's not necessary to declare that it throws this Exception. I noticed that it depends on the exception itself. example:

class A {
public void doA() { throw new Exception("Exception"); }

public static void main(String[] args) {
A a = new A();
try{
a.doA();}
catch(Exception e){ }
}
}


This code fails to compile because there is unreported Exception. But if I changed the Exception to NullPointerException then it will compile as in:

class A {
public void doA() { throw new NullPointerException("Exception");}

public static void main(String[] args) {
A a = new A();
try{
a.doA();}
catch(Exception e){ }
}
}


So what kind of exceptions that require method to declare that it throws exceptions?

Thanks for your time

admin
Site Admin
Posts: 10046
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: A question about exceptions

Post by admin »

The rule is very simple. Any exception class that extends from RuntimeException or Error need not be declared in the throws clause. These exceptions are called unchecked exceptions because you don't have to check/acknowledge the possibility of them being thrown. NullPointerException is such an exception.

All other exceptions have to be either caught in a try/catch block or have to be declared in the throws clause. These are therefore called checked exceptions. java.lang.Exception is a checked exception.

There is a small write up on common exception classes in the Study Refs section of the simulator. You might want to take a look at that as they are important for the exam.

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

Adlearee
Posts: 1
Joined: Sat May 19, 2012 2:52 am
Contact:

Re: A question about exceptions

Post by Adlearee »

The answer is correct but i will try to put it even more simply than this. There are two classes of exceptions - checked and unchecked. Checked exceptions are the ones which the compiler acutally checks for and these have to be either handled in try catch blocks, or declared to be thrown. Unchecked exceptions are no problems. The code will work even wiothout decalring them as thrown.

Jix
Posts: 14
Joined: Thu May 31, 2012 7:57 pm
Contact:

Re: A question about exceptions

Post by Jix »

Ok thanks a lot, I tried now to check the reference but it told me that Access is denied.

admin
Site Admin
Posts: 10046
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: A question about exceptions

Post by admin »

Jix wrote:Ok thanks a lot, I tried now to check the reference but it told me that Access is denied.
It is trying to extract the document and unable to create it in the local file system. Try running the application as administrator.
If you like our products and services, please help us by posting your review here.

Jix
Posts: 14
Joined: Thu May 31, 2012 7:57 pm
Contact:

Re: A question about exceptions

Post by Jix »

It worked, thanks

Post Reply

Who is online

Users browsing this forum: No registered users and 58 guests