aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes checked exceptions! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "checked exceptions!" Watch "checked exceptions!" New topic
Author

checked exceptions!

ashok khetan
Ranch Hand

Joined: Jul 29, 2001
Posts: 153
class MyClass {
void show() {
try {
int k = 20/0;
} catch(RuntimeException e) {
if(5>2)
throw new IllegalAccessException();
}
finally {
System.out.println("in finally");
}
}
public static void main(String args[]) {
new MyClass().show();
}
}
__________
the above pragram gives a compile-time error becos, a checked exception can't be thrown from the catch block directly(or it must be declared in the throws clause of the method).
here is another code:
_________
class MyClass {
int show() {
try {
int k = 20/0;
return 10;
} catch(RuntimeException e) {
if(5>2)
throw new IllegalAccessException();
return 4;
}
finally {
System.out.println("in finally");
return 30;
}
}
public static void main(String args[]) {
new MyClass().show();
}
}
_________
In the above code, a checked exc. is being thrown, and no throws clause in the method! but it compiles well. I couldn't understand why does it not give an error???
thanx.
ashok.
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Hi Ashok,
In the second code the checked exception is NOT thrown ! In fact,
the throw statement is executed but then the control passes to the finally block and since the finally "returns" 30 the exception is discarded and the method returns normally. This is my understanding.
I think this is almost the same as when you have, say, three catch blocks and each of them throws an exception (say unchecked), the control then passes to the finally block (if there is one). If the finally block happens to throw an exception as well then any exception thrwon before is discarded. As a rule of thumb, it is always the last exception being thrown that gets really thrown.
Hope that helps,
Val


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
I agree Val. The finally clause is executed before any outing way of the code within a try or catch (except maybe System.exit()) , and if the finally doesn't return control didn't go back


SCJP2. Please Indent your code using UBB Code
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
True jose,
I forgot to write about System.exit() .
Val
Nguyen Trung
Greenhorn

Joined: Aug 16, 2001
Posts: 6
hi Ashok, Valentin C. and Jose
at first i completely agree with Valentin and Jose. But
when i try to compile the second code fragment using JDK version 1.3. It DOES give compiler-error:
"unreported exception java.lang.IllegalAccessException; must be caught or declared to be thrown"
Plz explain why ?
thanx
james hoskins
Ranch Hand

Joined: Jun 28, 2001
Posts: 31
Hello Nguyun,
I believe the code should throw an exception, even if the finally block returns a value; returning a value surely has nothing to do with exception handling!
As far as I understand it, an exception can be thrown in the try block and caught, but if another exception is thrown in the catch block then the finally block is executed and the exception passes to the default exception handler. If a checked exception is thrown in the catch block then surely you must declare it according to the JLS.
Perhaps some compilers are not as rigerous as others!
Kind regards,
james.
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
to Trung
the compiler must check for checked exceptions so you obtain the error regardless the fact that at run time the exception is not thrown. The compiler doesn'check that.
to James
Whenever a code whithin a try o cacth clause is going to jump out of the clause the finally code is executed. The finally clause is like a mini subroutine after it has executed the control returns to the instruccion that was going to leave the block. I mean that finally is executed before the exception is thrown. But if the finally does not complete normally, it doen't return and the the code that throws the exception is not executed.
This is explained in Bill Venners' boook Inside the Java 2 virtual Machine
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: checked exceptions!