This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Exception Handling

 
tony james
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 782
Chrome Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Dave Vick
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
William Barnes
Ranch Hand
Posts: 986
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic