Originally posted by Sadanand Murthy:
[ January 26, 2004: Message edited by: John Ryan ]<hr></blockquote>
In your main() method where you catch it & call handleException(), you are calling handleException() with the argument ex which is of type Exception. Due to early binding, the compiler knows that ex is of type Exception & therefore at compile time (early binding) the link is made to the handleException() method which takes an argument of type Exception.
If you want the binding to be done at runtime instead of compile time try to declare the catch(Exception ex) as catch(Object ex); but then you will need to use instanceof to determine the type of object ex is then call handleException by explicitly casting ex to that type. I must mention here that I've not tried this myself & so don't know for sure whether this will work or not.[/QB]
Okay thanks Sadanand.
I tried something else whereby the exception thrown by my utility class (ExceptionX) inherits from a futher exception ExceptionY. ExceptionY in turn inherits from ExceptionZ. My utility class now declare that it throws ExceptionY, while internally it actually throws ExceptionX. Within my main method I catch ExceptionY.
I have two handleExceptionMethods - one which takes ExceptionX and one which takes ExceptionZ.
However the method which handles ExceptionZ is always called. Is this Early Binding also?
Does the compiler work out that while it catches ExceptionY and calls a handleMethod, becuase there is no handle method which accepts an ExceptionY so it will always use the Handle method that accepts ExceptionZ (rather than the one for ExceptionX which I was still hoping for). Is this because ExceptionY inherits from ExceptionZ and therefore "is" of type ExceptionZ
Thanks for the help. This is bugging me a lot....