Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Exception Handling

 
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
 
Ranch Hand
Posts: 782
Python Chrome 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
 
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
 
Ranch Hand
Posts: 1067
2
IntelliJ IDE Spring Java
  • 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.
 
    Bookmark Topic Watch Topic
  • New Topic