• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Try Catch Exceptions

 
Joanne Fire
Ranch Hand
Posts: 33
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9059
12
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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).]
 
William Barnes
Ranch Hand
Posts: 986
Eclipse IDE Firefox Browser Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Joanne Fire
Ranch Hand
Posts: 33
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 62
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 986
Eclipse IDE Firefox Browser Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
(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
Posts: 33
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic