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
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
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.