Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt in Exceptions.

 
Rahul Siddharth
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • 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?.
 
Nimit Shah
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Result will be 13423
 
Rahul Siddharth
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
Deepak Jain
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Thanks to Deepak and Nimit.
 
Rahul Siddharth
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • 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.
 
dolly shah
Ranch Hand
Posts: 383
  • Mark post as helpful
  • send pies
  • 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
  • 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?.
 
Paul Clapham
Sheriff
Pie
Posts: 20718
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic