• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Exception logic.

 
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 4982
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 384
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
When people don’t understand what you are doing they call you crazy. But this tiny ad just doesn't care:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic