• 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
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Exceptions & Overridding Example

 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am trying to relate the example from Chapter5 of K&B with one of the examwatches in Chapter2 (Page 104-Examwatch & Page Page390- Q3). But I have difficulty fixing the unreported exception error while compiling. Can anybody please explain.
Two topics involved here are
1. Overridding a method (I think I understood this right)
Overridding a method of the parent class which declares a checked exception with a method in the subclass that may or may not declare the exception. But if the overridden method declares an exception, it can be a run-time exception or an exception that is not broader than the parent class method.
2. Polymorphic super type reference referring to the subclass type (I still can't understand why the compiler cannot recognize this as the parent class method at compile time. Can somebody please explain this also)

 
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
dude at compile time the compiler only knows about the reference variable and that is of Master type....and it can only assume that you are going to call the master method....it is only at the runtime that the jvm knows about the object and accordingly calls the appropriate method

but here at the compile time...you have declared that your master method throws an exception...since your method is throwing a checked exception....you have to put a try catch block around the line which calls the method...
 
Ankur kothari
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
this compiles because now the compiler doesnt have a problem since you are catching the error....or what you can do is declare that main method throws filenotfoundexception which is called ducking the exception
 
Esther Kak
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your help
 
Ankur kothari
Ranch Hand
Posts: 531
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
anytime...hope you understood
 
Ranch Hand
Posts: 400
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ankur kothari wrote:this compiles because now the compiler doesnt have a problem since you are catching the error....or what you can do is declare that main method throws filenotfoundexception which is called ducking the exception



yes ankur is correct, it is because object create at run time not compile time...at the time of compilation, the compiler think this the method of Master class and it should be catch or throws the exception.
reply
    Bookmark Topic Watch Topic
  • New Topic