my dog learned polymorphism*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Exception logic. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Exception logic." Watch "Exception logic." New topic
Author

Exception logic.

Vishy Karl
Ranch Hand

Joined: Sep 08, 2003
Posts: 116
Hi all,
look at the following 2 examples...
Example 1

What is the result of attempting to compile and run the program?
a. Prints: 0,1,0,0
b. Prints: 1,1,0,1
c. Prints: 0,1,0,1
d. Prints: 0,1,1,1
e. Prints: 1,1,1,1
f. Compile-time error
g. Run-time error
h. None of the above
Answer is ---
f Compile-time error A compile-time error is generated, because the second catch clause attempts to catch an exception that is never thrown in the try block.

Example 2

What is the result of attempting to compile and run the program?
a. Prints: 0,1,0,0
b. Prints: 1,1,0,0
c. Prints: 0,1,1,0
d. Prints: 1,1,1,0
e. Prints: 1,1,1,1
f. Compile-time error
g. Run-time error
h. None of the above
Answer is ---
c Prints: 0,1,1,0
Why does the compiler not complain about the WhiteException that is not thrown as it complains in example 1 for BlueException??
Am i missing something ??
thanks,
[ Jess adjusted where the [code] tags end so that word wrapping doesn't get all screwed up with the text ]
[ January 31, 2004: Message edited by: Jessica Sant ]

"The man who can drive himself further once the effort gets painful is the man who will win." <br />Roger Bannister
Nicholas Cheung
Ranch Hand

Joined: Nov 07, 2003
Posts: 4982
Hi Vishy,
In the 1st example, since there is no inheriance relationship between RedException and WhiteException and m1() does NOT throw BlueException, thus, if you catch the BlueException in a try-catch-block, the compile sure complain that, BlueException is never thrown.
In the 2nd example, on the other hand, there DOES inheriance relationship beteweeh ColorException and WhiteException. The relationship is WhiteException extends ColorException.
In method m1(), it declares to throw ColorException. According to the Java rule, if a method that declare to throw Exception E, if E (or exception classes that extend E [subclasses of E]) is encountered, it will be thrown without any problems, and thus, the try-catch-block of m1() has no problem.
Does this make sense?
Nick.


SCJP 1.2, OCP 9i DBA, SCWCD 1.3, SCJP 1.4 (SAI), SCJD 1.4, SCWCD 1.4 (Beta), ICED (IBM 287, IBM 484, IBM 486), SCMAD 1.0 (Beta), SCBCD 1.3, ICSD (IBM 288), ICDBA (IBM 700, IBM 701), SCDJWS, ICSD (IBM 348), OCP 10g DBA (Beta), SCJP 5.0 (Beta), SCJA 1.0 (Beta), MCP(70-270), SCBCD 5.0 (Beta), SCJP 6.0, SCEA for JEE5 (in progress)
Sudhakar Krishnamurthy
Ranch Hand

Joined: Jun 02, 2003
Posts: 76
Hello:
i think the answer should be "complie-time error" since m2 just declares to throw a whiteException and never throws it in the method body. Can anyone explain if i am missing something here???
Thanx
-S
Davy Kelly
Ranch Hand

Joined: Jan 12, 2004
Posts: 384
Remember that the method has declared that it might throw the exception, this is enough for the compiler.
either use try/catch or declare it.
the throw keyword makes the exception happen, whereas throws says it might throw, but it might not.
hope this helps.
davy


How simple does it have to be???
 
Consider Paul's rocket mass heater.
 
subject: Exception logic.
 
Similar Threads
Exception- Doubt Help Please....
Exceptions
dan exam doubt 15
doubt on catching exception on following code
Exception Question from Dan Chisholm