• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Exception Base classes

 
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
Just a question on Exception handling which I am having some trouble getting my head around.
I understand that it is possible to throw an exception and if a handler is declared which catches a base class of this exception then it will be caught there. What I am wondering is what is the type of my exception at that point in time. The reason I ask is that I want to carry out some actions polymorphically using the exception and it isnt working as I expected. Some code might best illustrate the issue



I have a utility class whose method interface declares that it throws a particular type of exception. However within the method an exception derived from the declared exception is thrown. An exception handler exists which catches the base exception type. Now if at the point where the exception is caught if I was to check the type of the exception using instanceof or ex.getClass().getName() it would tell me that its type was ExceptionX. However when I then call handleException passing the caught exception it prints out "Handler for Exception". While I know that my handler is for the base exception Im confused by the fact that I know its type is a derived exception yet the handler called is still for the base exception.
Any pointers would be appreciated. At one level it makes sense to me given the way my handler is declared. At another it makes no sense given the results when I get its type....
John
[ January 26, 2004: Message edited by: John Ryan ]
 
Ranch Hand
Posts: 382
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by John Ryan:


[ January 26, 2004: Message edited by: John Ryan ]


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.
 
John Ryan
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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....
 
Sadanand Murthy
Ranch Hand
Posts: 382
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by John Ryan:
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....


I believe it is. Since at compile time it knows that the called method throws ExceptionY but it is not getting caught; it does not know that ExceptionX is what is actually thrown (based on the signature of the called method). So, it binds to the catch(ExceptionZ) since the compiler knows that Y is of type Z.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic