• 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
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Throwing exception in overriding methods

 
Ranch Hand
Posts: 469
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

The above code compiles & runs successfully.Shouldn't it give compiler error due to try catch block in line1?Coz Exception that is never thrown is caught?
Veena
[ edited to format code for easier reading -ds ]
[ September 15, 2003: Message edited by: Dirk Schreckmann ]
 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Shouldn't it give compiler error due to try catch block in line1?Coz Exception that is never thrown is caught?


The variable myref is a reference of type TechnoSampleSub. The method test is called on an object of TechnoSampleSub type.
This throws no exception and hence no problems. Even if an excpeion were to arise, it would be caught by the catch block specified.
Regards,
Anupreet
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Veena,
Compiler will not display any error even though java.lang.Exception is not thrown by the test() method because of the following reason:
1) java.lang.Exception class is a parent class of unchecked (RuntimeException, ) and checked exceptions. So there are chances that the code written in the try block throws any of the unchecked exceptions.So the compiler will never throw an error.
Note: Even if there is no code in try block and Exception object is caught in the catch block , the compiler will never complain.
But if we catch IOException(checked Exception) , then the compiler gives the error that
"exception java.io.IOException is never thrown in body of corresponding try statement".
This is because IOException is not thrown by any of statements in the try block.

I hope this clarifies your doubt.
Regards,
Cody
 
Veena Pointi
Ranch Hand
Posts: 469
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Cody.It is clear now.
reply
    Bookmark Topic Watch Topic
  • New Topic