• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

exception trouble

 
Ranch Hand
Posts: 191
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


when the above code is run, value 5 is printed. but when i am replacing finally block with return x--; statement, ArithmeticException is thrown at run-time.

why run-time exception is not thrown when we included finally block?
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Vikas,

In Catch block you do have a statement "x = x % 0;" which also throws excetion.So if you remove finally block then code will throw java.lang.ArithmeticException: / by zero for sure

Regards,
Abhishek
 
vikas sharmaa
Ranch Hand
Posts: 191
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by abhishek patil:
Hi Vikas,

In Catch block you do have a statement "x = x % 0;" which also throws excetion.So if you remove finally block then code will throw java.lang.ArithmeticException: / by zero for sure

Regards,
Abhishek



but why? statement inside the catch block throws ArithmeticException. so finally block should be executed first and then exception should be thrown.

as in the below code


[ July 10, 2007: Message edited by: vikas chess ]
 
vikas sharmaa
Ranch Hand
Posts: 191
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
still waiting for response...
 
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by vikas chess:
still waiting for response...

Short answer: It's a bad idea to have a return statement in a finally block.

Long answer: Have a look at 7.13 Compiling finally in the Java Virtual Machine Specification.
 
abhishek patil
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Vikas,

class Test{
static int call(int x) {
try
{
x = x / 0;
}catch(Exception e){
x = x % 0;
}finally{
return x--; // here method is returning a value.
}
}

In the above method we have a return statement in finally. As we know if either try or catch bolck throws exception then finally will be executed first. So the method returns to the main without throwing exception.


try{
int x = 10 / 0;
}finally{
System.out.println("here");
}

here as return type would be void so exception is thrown after displaying sysout.
 
vikas sharmaa
Ranch Hand
Posts: 191
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks abhishek and Manfred. i understood it now.
 
"Don't believe every tiny ad you see on the internet. But this one is rock solid." - George Washington
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic