File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Exceptions - Basics

 
Esther Kak
Ranch Hand
Posts: 51
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
    Posts: 385
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
     
    Esther Kak
    Ranch Hand
    Posts: 51
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 9497
    22
    Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
     
    Siva Masilamani
    Ranch Hand
    Posts: 385
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 51
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks.
     
    avi sinha
    Ranch Hand
    Posts: 453
    Google Web Toolkit Hibernate Java
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
     
    I agree. Here's the link: http://aspose.com/file-tools
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic