wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Throws exception in main() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Throws exception in main()" Watch "Throws exception in main()" New topic
Author

Throws exception in main()

Michelle Chang
Greenhorn

Joined: Jan 22, 2010
Posts: 8

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?





Always speak up for stupid questions.
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

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

Joined: Nov 04, 2009
Posts: 2066

Hi, Michelle Chang

For the 3rd question, Check the Specification/rules of overriding a method, And ArithmeticException is a Runtime Exception!


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Prithvi Sehgal
Ranch Hand

Joined: Oct 13, 2009
Posts: 774
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,



Prithvi,
My Blog, Follow me on Twitter,Scjp Tips, When you score low in mocks, Generics,Scjp Notes, JavaStudyGroup
bhanu chowdary
Ranch Hand

Joined: Mar 09, 2010
Posts: 256
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

Joined: Jan 28, 2008
Posts: 5575


throwing a runtime exception with method signature is bad Idea, shortly *throws* should use with Checked Exception.
narendra bhattacharya
Ranch Hand

Joined: Feb 17, 2010
Posts: 66
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....


SCJP1.6,SCWCD1.5
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Throws exception in main()