aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Doubt in Exceptions. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Doubt in Exceptions." Watch "Doubt in Exceptions." New topic
Author

Doubt in Exceptions.

Rahul Siddharth
Ranch Hand

Joined: Feb 17, 2007
Posts: 33
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

Joined: Jul 02, 2007
Posts: 53
Result will be 13423
Rahul Siddharth
Ranch Hand

Joined: Feb 17, 2007
Posts: 33
Nimit Could you please explain me how?.There is a statement after finally right?.Why is it skipped?
Nimit Shah
Ranch Hand

Joined: Jul 02, 2007
Posts: 53
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

Joined: Aug 05, 2006
Posts: 637
Just to add , after executing the statmeents in the finally () the control returns even if there are statements after finally block.
Thanks
Deepak


SCJP, SCWCD, SCBCD
Rahul Siddharth
Ranch Hand

Joined: Feb 17, 2007
Posts: 33
Thanks to Deepak and Nimit.
Rahul Siddharth
Ranch Hand

Joined: Feb 17, 2007
Posts: 33
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

Joined: Jun 18, 2007
Posts: 383
How you guys can compile this code? It gives me multiple error.


SCJP-1.5<br />SCWCD-1.4
Rahul Siddharth
Ranch Hand

Joined: Feb 17, 2007
Posts: 33
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
Bartender

Joined: Oct 14, 2005
Posts: 18129
    
    8

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doubt in Exceptions.
 
Similar Threads
finally doubt
Netbeans GUI Code Doubt
Exception
Doubt in static...
Doubt