• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Doubt in Exceptions.

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Folks.I have a doubt in the following code.

code:
---------------------------------------------------------------------------
public class Alpha {
public static string output = �� ;

public static void foo(int i) {
try {
if(i == 1) {
throw new Exception ();
}
output + = �1�;
}
catch(Exception e) {
output + = �2�;
return;
}
finally {
output + = �3�;
}
output + = �4�;
}

public static void main (String args[]) {
foo(0);
foo(1);
}
}
----------------------------------------------------------------
When foo(0) is called it prints 134.When foo(1) is called, what does it print 23 or 234?.I was confused about this.I believe it should be 134234(on the whole).Am I correct?.
 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Result will be 13423
 
Rahul Siddharth
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nimit Could you please explain me how?.There is a statement after finally right?.Why is it skipped?
 
Nimit Shah
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is skipped because of the return statement in catch.

when you do f(1), try block throws Exception.
Catch block appends 2 to String then call return, but finally is executed before method returns.
 
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just to add , after executing the statmeents in the finally () the control returns even if there are statements after finally block.
Thanks
Deepak
 
Rahul Siddharth
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks to Deepak and Nimit.
 
Rahul Siddharth
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Deepak.If you dont mind, could you please tell me why the program below prints only "Arithmetic Exception".I thought it should print "Finshed" as well.
code:
---------------------------------------------------------------------
try {
int x = 0;
int y = 5 / x;
} catch (Exception e) {
System.out.println(�Exception�);
} catch (ArithmeticException ae)
System.out.println(�Arithmetic Exception�);
}
System.out.println(�finished�);
------------------------------------------------------------------------
Thanks in Advance.
 
Ranch Hand
Posts: 383
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How you guys can compile this code? It gives me multiple error.
 
Rahul Siddharth
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This was just a code snippet.May be thats why you are getting errors.Could someone explain me why "Finished"( along with Arithmetic Exception) wasnt printed in the code above?.
 
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by rahul siddharth:
Could someone explain me why "Finished"( along with Arithmetic Exception) wasnt printed in the code above?

It isn't. Nothing is printed. As dolly shah said, it doesn't even compile.

Even after I fixed the quotes to be proper quotes and inserted the missing { character, it still wouldn't compile because you can't catch those two exceptions in that order.

There is no point in asking questions about what happens when you run code that can't be compiled. But if you would like to post a small compilable example to discuss, please do that.
 
reply
    Bookmark Topic Watch Topic
  • New Topic