GeeCON Prague 2014*
The moose likes Beginning Java and the fly likes Exception Handling Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Exception Handling" Watch "Exception Handling" New topic
Author

Exception Handling

tony james
Greenhorn

Joined: Jul 24, 2001
Posts: 12
Hi,
pls explain how and why the following code works :

My question is regarding the statements marked A and B.
fillInStackTrace( ) produces a reference to a Throwable object .But the catch clause in main handles 'Exception'.Why isnt there any problem? Also, what is the role of 'throws Throwable ' here?
Thanx in advance...
tony
Pho Tek
Ranch Hand

Joined: Nov 05, 2000
Posts: 761

Tony,
>what is the role of 'throws Throwable ' here
It's a java syntax to allow the compiler to check that
the method handles the exception locally or propagates it up the call stack. They're called Checked Exceptions. And anything that's not a subclass of RuntimeException is a checked exception.
>fillInStackTrace( ) produces a reference to a
>Throwable object .But the catch clause in main
>handles 'Exception'.Why isnt there any problem?
Exception is a subclass of Throwable. So I believe downcasting
allows the Throwable to be casted to an exception ? Maybe someone else will correct me..
Pho

Regards,

Pho
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
Tony and Pho
Two points:
Pho mentioned that anthign that is not a subclass of RunTimeException is checked. In addition anything that is a subclass of Error is also not checked.
If you look a the API for fillInStackTrace( ):

Fills in the execution stack trace. This method records within this Throwable object information about the current state of the stack frames for the current thread.
Returns:
this Throwable object.

So it returns the same object, it doesn't change it to a different type.

hope that helps
Dave


Dave
William Barnes
Ranch Hand

Joined: Mar 16, 2001
Posts: 986

I think the answer to:

fillInStackTrace( ) produces a reference to a Throwable object. But the catch clause in main handles 'Exception'.Why isnt there any problem?

is that the line of code:
"throw e.fillInStackTrace(); // ***A"
is never executed. Control leaves the method on the "throw" and returns to main, and is caught there.


Please ignore post, I have no idea what I am talking about.
 
GeeCON Prague 2014
 
subject: Exception Handling