File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Throws exception in main()

 
Michelle Chang
Greenhorn
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • 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?



 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • 0
  • Mark post as helpful
  • send pies
  • 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 ?
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • 0
  • Mark post as helpful
  • send pies
  • 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!
 
Prithvi Sehgal
Ranch Hand
Posts: 774
  • 0
  • Mark post as helpful
  • send pies
  • 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,


 
bhanu chowdary
Ranch Hand
Posts: 256
  • 0
  • Mark post as helpful
  • send pies
  • 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 Java Windows XP
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

throwing a runtime exception with method signature is bad Idea, shortly *throws* should use with Checked Exception.
 
narendra bhattacharya
Ranch Hand
Posts: 66
  • 0
  • Mark post as helpful
  • send pies
  • 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....
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic