wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Exceptions - Basics 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 "Exceptions - Basics" Watch "Exceptions - Basics" New topic
Author

Exceptions - Basics

Esther Kak
Ranch Hand

Joined: Oct 11, 2006
Posts: 51
I think I have to get my basics right with Exceptions. Please help and correct if I am not right.
Example code

  • If a riskymethod() throws a CheckedException, duckingmethod() calls the riskymethod() and does not "handle&declare" the exception, ---compile time error occures(JVM says unreported exception...)
  • If a riskymethod() throws a uncheckedException, duckingmethod() calls the riskymethod(() and does not "handle&declare" the exception thrown by the riskymethod() --- compiles fine---but run time error occurs
  • If a riskymethod() throws an Error, duckingmethod() passes the Error to the main() and does not "handle&declare" the Error thrown by the riskymethod() - compiles fine --but runtime error occurs


  • Siva Masilamani
    Ranch Hand

    Joined: Sep 19, 2008
    Posts: 385
    Yes you are right.

    If any of the method contains code that may throw checked exception then those codes should be inside try/catch block or the method itself should duck that checked exception.

    Similarly if you are calling a method which may throw checked exception then the calling code should be inside try/catch block or the caller method itself should duck that exception.

    But it is not necessary to catch or duck unchecked exception or Error even if you declare statement like throw new RuntimeException();

    One more thing if you are either using try/catch block or ducking checked exception then that method should contain some code that throws checked exception.(method disp)

    But Exception and the above classes are little bit different ,they are partially checked and unchecked exception.

    That is it can contain try/catch block inside a method even though the method does not throw Exception.See disp1.

    E.g



    Hope it should have cleared your doubt


    SCJP 6,SCWCD 5,SCBCD 5

    Failure is not an option.
    Esther Kak
    Ranch Hand

    Joined: Oct 11, 2006
    Posts: 51
    I really appreciate your response. But I have one more question before I close with exceptions.
    As you said that we have two options
    1. use try-catch code for exception
    2. duck the checked exception

    Here is the example for option1. In this example ---filenotfound is a checked exception.
    Can you please help me modify the handlingMethod() to duck the checked exception (that means to use option2)

    Ankit Garg
    Sheriff

    Joined: Aug 03, 2008
    Posts: 9303
        
      17



    SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
    Siva Masilamani
    Ranch Hand

    Joined: Sep 19, 2008
    Posts: 385
    OK you already ducked that exception in the riskyMethod so you need not to catch that exception in the riskyMethod since you are ducking that exception, in the handling method you should put the calling code inside try/catch which you did already or declare the handling method itself to duck that exception using throws clause.

    If you do this then you have to put the calling code in the main method inside try/catch or declare main itself to duck that exception in which case JVM will handle that exception and terminate the program.

    So if you are ducking an exception it will fall through the calling stack until it is either handle by trt/catch block or by JVM itself.
    Esther Kak
    Ranch Hand

    Joined: Oct 11, 2006
    Posts: 51
    Thanks.
    avi sinha
    Ranch Hand

    Joined: Mar 15, 2009
    Posts: 453

    one more thing and of course an important point related to it.

    polymorphism works for the overridden method but it is resolved at runtime.
    so in case when the superclass method declares a checked exception and the overridden method in subclass doesn't declare it
    if we polymorphically call the subclass method using superclass reference , despite the method in subclass will be called we will have to handle or declare it to
    get the code compiled sucessfully

    example code



    avi sinha


    SCJP 5.0 SCWCD 5.0
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Exceptions - Basics