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
A question about exceptions
Moderator: admin
Online
-
- Site Admin
- Posts: 10058
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: A question about exceptions
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.
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.
-
- Posts: 1
- Joined: Sat May 19, 2012 2:52 am
- Contact:
Re: A question about exceptions
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.
-
- Posts: 14
- Joined: Thu May 31, 2012 7:57 pm
- Contact:
Re: A question about exceptions
Ok thanks a lot, I tried now to check the reference but it told me that Access is denied.
Online
-
- Site Admin
- Posts: 10058
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: A question about exceptions
It is trying to extract the document and unable to create it in the local file system. Try running the application as administrator.Jix wrote:Ok thanks a lot, I tried now to check the reference but it told me that Access is denied.
If you like our products and services, please help us by posting your review here.
-
- Posts: 14
- Joined: Thu May 31, 2012 7:57 pm
- Contact:
Re: A question about exceptions
It worked, thanks
Who is online
Users browsing this forum: admin and 38 guests