Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Spring in Action (5th edition) this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

checked exceptions!  RSS feed

 
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Gold Digger
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Valentin Crettaz
Gold Digger
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
True jose,
I forgot to write about System.exit() .
Val
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!