aspose file tools*
The moose likes Beginning Java and the fly likes Try Catch Exceptions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Try Catch Exceptions" Watch "Try Catch Exceptions" New topic
Author

Try Catch Exceptions

Joanne Fire
Ranch Hand

Joined: Nov 22, 2001
Posts: 33
Hello can anyone help me understand how try-catch works?
In the following code, what appears on the screen if
an exception of the class Shoot is thrown at the call
of the method GoAheadShoot()?
Thanks!

[This message has been edited by Cindy Glass (edited November 26, 2001).]
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9047
    
  10

Joanne,
Your code will be easier to read if you put [ code][ /code](without the spaces) tags around it. You can click on the pencil and paper icon of this post to see how I did it. This code is too involved to give a detailed explanation of everything going on here.

Basically, execution starts with a try block.

If something in the try block throws an exception, execution stops at that point and goes directly to the catch block (if there is one). Then, whether or not there was a catch block, execution proceeds to the finally block.

If nothing in the try block throws an exception, execution completes in the try block and proceeds to the finally block.

[This message has been edited by Marilyn deQueiroz (edited November 24, 2001).]


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
William Barnes
Ranch Hand

Joined: Mar 16, 2001
Posts: 986

In your code example you have three try and catch blocks, which apply to the call to GoAheadShoot(). You catch 1] arrayOutOfBound, 2] Shoot and 3] Exception.
Since a "Shoot" exception is being generated the 'Shoot catch' code will be executed. The 'Shoot catch' code has its own try catch blocks - which is what makes this example way too complicated. If you are starting to learn try catchs you need something simpler.
So you get into the 'Shoot catch' code and as long as you don't have any problems printing to system out you will get "Shoot me" on the screen. Thats it.


Please ignore post, I have no idea what I am talking about.
Joanne Fire
Ranch Hand

Joined: Nov 22, 2001
Posts: 33
Thank you for all that have replied!
.. Joanne
Originally posted by Joanne Fire:
Hello can anyone help me understand how try-catch works?
In the following code, what appears on the screen if
an exception of the class Shoot is thrown at the call
of the method GoAheadShoot()?
Thanks!
try
{
try
{
try
{
GoAheadShoot();
}
catch (ArrayOutOfBound e)
{
System.out.println("Shoot And Catch");
}
}
catch (Shoot e)
{
try
{
System.out.println("Shoot Me");
}
catch (Exception error)
{
System.out.println("Shoot Again");
}
finally
{
throw e;
}
}
}
catch (Exception e)
{
System.out.println("Enough");
}

Gaia Nathan
Ranch Hand

Joined: Aug 01, 2001
Posts: 62
Hi Christopher,
Doesn't "Enough" get printed out as well? Can you show me the flow of control in the code?
Thanks.
Gaia.
William Barnes
Ranch Hand

Joined: Mar 16, 2001
Posts: 986

(Since I am posting again.) Joanne please don't include the entire previous post, people can scroll up to find it. Thanks.
Disclaimer - I don't like this example, it is overly confusing and more complex than anything I have seen in the real world.
To answer the question:

Doesn't "Enough" get printed out as well?

Yes, that does appear to be the case. And is another reason I don't like this example. Why would anyone throw out of a "finally" block? You are about to leave anyways.
Joanne Fire
Ranch Hand

Joined: Nov 22, 2001
Posts: 33
Christopher,
You are right, this example is not the best, ironically, it was on the entry exam for one of the largest consulting firm!
Thanks for the help.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Try Catch Exceptions