• 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
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

Exception's Question

 
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

I found this question on one of the Dan's exam's. Below is the code..



Answer given is: Compile-time error.
Reason: the second catch clause attempts to catch an exception that is never thrown in the try block.

Can anybody put some more light onto it. Should every caught exception be explicitly thrown?

Thanks a lot,
kits
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, It is not compulsary that every exception should be thrown for further refrence you can go in java tutorials in exception handling
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Answer given is: Compile-time error.
Reason: the second catch clause attempts to catch an exception that is never thrown in the try block.

[/QB]

Hi Kitty,

Code snippet of Dan's exam uses checked exceptions(RedException & BlueException) i.e subclass of class Exception.
Checked exceptions represents invalid conditions which are outside the immediate control of the program(e.g. Database problems, Network breakdowns etc.)
For all checked exceptions methods are obliged to establish a policy for all checked exceptions thrown by its implementation
(either pass the checked exception further up the stack, or handle it somehow).
This is why if you expect method m1 to throw BlueException either include it in signature of method m1 or make it unchecked exception by extending it from RuntimeException.

Regards,
Kapil
 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
quick hack here,

RuntimeException,Error and all their subclass are unchecked exceptions .
meaning :
if these exceptions are thrown and not caught - its ok
if these exceptions are not thrown and but caught - even then its ok

-------------------
rest all exception are checked exceptions

if these exceptions are thrown and not caught - its NOT ok
if these exceptions are not thrown and but caught - even then its NOT ok

-------------------
EXCEPTION to this rule

Exception, Throwable are both checked exceptions BUT

if these exceptions are not thrown and but caught - then its ok

--------------------

if you can remember this you can pass SCJP
if you want to know why ? then this might help http://www.javaworld.com/javaworld/javatips/jw-javatip134-p2.html

Hope that helps
Rafay
[ August 20, 2004: Message edited by: Abdul Rafay Mansoor ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic