• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Rob Spoor
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Holloway
  • Piet Souris
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Himai Minh

throw in finally clause

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Look at following example:
class MyException extends Exception{}

//some other codes here-----
public void myfirst() throws MyException{ //line 10
try{
mysecond();
}catch(MyException e){
} finally{
throw new MyException(); //line 15
}
}//end of myfirst

public void mysecond () throws MyException {
System.out.println("myexception");
}//end of my second

//----rest of the other code

my question: if we compile the code, we will get compile error to say that finally clause line 15 could not complete normally. Why this happens? I already declare throws MyException in method myfirst(). however if I put
mysecond(); instead which throws MyException, it works fine. Does it mean I can not put an explicit throw in finally clause even the exception is declared in the method?
 
Ranch Hand
Posts: 132
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried to used your code and not getting any problem as you mentioned.
Here is sample code i used..
 
Yun Hao
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Very interesting. I tried the code Nitin Bhagwat put again. I still got the same compiler error "finally clause could not complete normally". In theory, it should not have such compiling error since the exception already declared. I am using javasdk1.4.2_04 running on windows2000, the command line i typed javac tst.java.
It is very strange same code got two different results. Is there anything wrong with my command although i don't think so.
can anyone else try this code again to see whether you get compile error or it is working okay for you also.
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yun,
The program got compile successfully on my machine.
Thanks
 
author
Posts: 23907
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Yun Hao:
Very interesting. I tried the code Nitin Bhagwat put again. I still got the same compiler error "finally clause could not complete normally". In theory, it should not have such compiling error since the exception already declared. I am using javasdk1.4.2_04 running on windows2000, the command line i typed javac tst.java.
It is very strange same code got two different results. Is there anything wrong with my command although i don't think so.
can anyone else try this code again to see whether you get compile error or it is working okay for you also.



This is obscure, and I don't track the specification that closely, so take this with a grain of salt. I think the specification don't allow "return" or "throw" in the "finally" clause because the exit has already been determined. What if the exit was already a throw? What do you do with the previous exception? Or even the response that it was meant to return?

In any case, I think they only recently fixed this. I would be interested to know what JDKs are being used for the cases where it does work.

Henry
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In the dim depths of my mind...
 
Yun Hao
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Barry. I am using jdk1.4.2. It looks like only new version jdk gave the compile error. I am going to take the Certification Exam. So I think if there is question like that in finally clause there is throw statement, I should choose compile fails.

Just to confirm, Premkumar and Nitin, by any chance if you check the message, can you tell me which jdk version you used to compile the code since you both didn't get any warning or error. Thanks in advance.
 
No one can make you feel inferior without your consent - Eleanor Roosevelt. tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic