• 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
  • Ron McLeod
  • Junilu Lacar
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Piet Souris
  • Carey Brown
  • Stephan van Hulst
Bartenders:
  • Frits Walraven
  • fred rosenberger
  • salvin francis

Throws exception in main()

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

My question is on page 421, Q15 Chapter 5 of KB's book. I simplified the question into several codes below.

My first question is that would this example 1 compile? If not, I need to throw RuntimeException, correct? If yes, how main handles the exception?

My second question is that would this example 2 compile?

In example 3, class B extends A, which does not declare any exception. However, the overriding method doStuff() declares an exception. Is it fine to have the overriding method doStuff() (B.doStuff()) to declare an exception without its overridden method (A.doStuff()) declaring an exception? why and why not?



 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
example1: it will compile , but throw a run time exception . and remember main method dont handle exception until you put try/catch, so simply it will throw the same

example2: it will compile . but dont handle the exception like example 1

By the way: it is simple to test/run . why cant you try yourself ?
 
Ranch Hand
Posts: 2066
IntelliJ IDE Clojure Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, Michelle Chang

For the 3rd question, Check the Specification/rules of overriding a method, And ArithmeticException is a Runtime Exception!
 
Ranch Hand
Posts: 774
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
1-Example 2 will compile fine and will throw a run-time Arthimetic Exception.
2-Example 3 is violating the rules for overriding. You are basically overriding a instance method and this overridden
method can't throw an exception because your parent method is not throwing any exception.

Hope this helps,


 
Ranch Hand
Posts: 256
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
example1: Will compile successfully, but, throws a RunTimeException when you run it.
example2: It will compile.
example3: will not compile as you have to declare the class A and the method both as abstract.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

throwing a runtime exception with method signature is bad Idea, shortly *throws* should use with Checked Exception.
 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually the overriding method may throw unchecked exception... no problem..

in example 3 void dostuff(); method def .. is wrong you must provide at least { }
now it will compile....
 
reply
    Bookmark Topic Watch Topic
  • New Topic