aspose file tools*
The moose likes Beginning Java and the fly likes Throwing/Catching Exceptions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Throwing/Catching Exceptions" Watch "Throwing/Catching Exceptions" New topic
Author

Throwing/Catching Exceptions

John Fairbairn
Ranch Hand

Joined: Jul 12, 2000
Posts: 31
Hi,
It's my understanding that a method can throw a CHECKED exception
only if it is declared in its throws clause. In this example, I am explicitly throwing the Exception object from the doThis method to the run method.
But, I'm not sure why the java compiler doesn't complain when the catch clause catches an ArithmeticException object and throws an Exception object.
Any help is appreciated.
Thanks.
class Except1 implements Runnable{
Thread t;
public static void main(String[] args)
{
Except1 a = new Except1();
a.go();
}
void go()
{
t = new Thread(this);
t.start();
}
public void run() {
try {
doThis();
}
catch(Exception e){
System.out.println("Caught:" + e.getMessage());
}
}
void doThis() throws Exception{
try {
}
catch(ArithmeticException e){
throw new Exception();
}
}
}





deekasha gunwant
Ranch Hand

Joined: May 06, 2000
Posts: 396
hi john,
u r right a method can throw a CHECKED exception
only if it is declared in its throws clause.
. but there are a few more things.
1) a method can throw an unchecked exception without declaring it.
2) a method can also throw an exception which is the sub class of the exception that it declares to throw. means if it declares
to throw IOException then it can always throw any sub class of IOException means MalformedURLException/EOFException or......
now in ur program
1) ArithmeticExceoption is an unchecked exception so even if the method had not declared 'throws Exception' throwing ArithmeticException would not have caused any error.
2) again in ur program if instead of Arithmeticexception if ur method throw any other checked exception even then ur program would not cause any error. coz Exception class is super class of all checked /unchecked exception. so since ur method says it throws 'Exception' it can legally throw any subclass of Exception class which include all checked and unchecked exception.
hope did not confuse u.
regards
deekasha

John Fairbairn
Ranch Hand

Joined: Jul 12, 2000
Posts: 31
I do understand your points about being able to throw unchecked exceptions or subclasses of the exception i'm throwing but i'm specifically wondering why the following clause does not cause the compiler to complain:
catch(ArithmeticException e){
throw new Exception();
}
The catch clause is expecting to catch an arithmetic exception and throws an exception... I must be confused and not understanding a concept.
Thanks
deekasha gunwant
Ranch Hand

Joined: May 06, 2000
Posts: 396
hi john,
u can throw any exception from catch block irrespective of the exception u r catching.
the only thing is that if u r throwing a checked exception then u need to either declare it or put it inside a try catch block.
in ur code
void doThis() throws Exception{
try {
}
catch(ArithmeticException e){
throw new Exception();
}
}
since u've declared in ur function signature that u will be throwing Exception . so u can throw it from anywhere inside that function (even from any damn catch block). modify ur function like
void doThis() throws ArithmeticException{
try {
}
catch(ArithmeticException e){
throw new Exception();
}
}

& it will start giving u compile time error.
i don't know if it helped u. i request other ranchers to come and help us out.
regards
deekasha
John Fairbairn
Ranch Hand

Joined: Jul 12, 2000
Posts: 31

Thanks deekasha.
So am i correct in saying that when explicitly throwing an exception, there will be no problem when:
(1) the exception being thrown (in throw clause) matches the exception declared in the throws clause matches or;
(2) the exception being thrown is a subclass of the exception declared in the throws clause
As long as these rules are followed for explicitly throwing exceptions, it doesn't matter what type of exception I am catching in the method throwing the exception (in this case ArithmeticException).
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Throwing/Catching Exceptions
 
Similar Threads
An "exceptional" error
Rethrow an exception
Exceptions
Exception Doubt pls help
sereral doubs... pls clear them all