Results 1 to 9 of 9
Like Tree1Likes
  • 1 Post By Tolls

Thread: How to add additional information to the exception object that can be retrieved later

  1. #1
    ouou is offline Member
    Join Date
    May 2011
    Posts
    4
    Rep Power
    0

    Default How to add additional information to the exception object that can be retrieved later

    In Java, I need to get the exact causes whether is FileNotFound or normal IOException. Which I mean if it is Filenotfound exception, do not loose the FilenotFound cause. If it is other IOException, show IOException. No overwritten the causes.

    I need to accurately describe the problem,consolidate the exceptions

    Important: I need the code block style in this required way.
    The original code block must follow in this way, beyond this, no requirement
    main(){
    try {
    readfile();
    }catch {
    }
    }

    readfile(){
    try{...
    }
    finally{
    if (br1 != null)
    br1.close();
    }
    }

    Above template cause a problem, in finally br1.close could possibly overwrite the FileNotException by generating a IOException.

    Here is my complete code:

    Java Code:
    import java.io.BufferedReader;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.IOException;
    
    class Hidden {
     String exceptionMessage=null;
      public static void main(String args[]){
        Hidden h = new Hidden();
            
        try {
         System.out.println("Main():: Try Block Starts");
         h.readFile();
         System.out.println("Main():: Try Block Ends");
        } catch (FileNotFoundException fne) {
         System.out.println("Main():: Catch FileNotFound Starts.");
         System.out.println("Hi, Caught FileNotFoundException!");
         System.out.println(fne.getMessage());
           //fne.printStackTrace();
        } catch (IOException ioe) {
         System.out.println("Main():: Catch IOException Starts.");
         System.out.println("Caught IOException!");
          System.out.println(ioe.getCause());
         //ioe.printStackTrace();
        }
      }
      
      @SuppressWarnings("finally")
    public void readFile() throws FileNotFoundException,IOException {
       BufferedReader br1 = null;
          FileReader fr = null;
        
          try {
           System.out.println("readFile():: Try Block Starts."); 
           fr = new FileReader("data4.fil");                       //1
           br1 = new BufferedReader(fr);
           int i = br1.read();                                     //2
           //Other code...
           System.out.println("readFile():: Try Block Ends.");                                   //4
         } catch (FileNotFoundException e){
          System.out.println("readFile():: Catch Block Starts.");
          exceptionMessage="data1.fil is not exisited *****From Catch Block's Catch";
          throw new FileNotFoundException( exceptionMessage);
          
        }
         finally {
           System.out.println("readFile():: Finally Block Starts");
           try{ 
             System.out.println("readFile()::Try in Finally Block Starts.");
             fr = new FileReader("data1.fil");
             if (br1 != null) 
                br1.close();
            //throw new IOException();
           }catch (IOException ioe){
              System.out.println("readFile():: Catch in Finally Block Starts.");
               if(ioe instanceof FileNotFoundException){
                exceptionMessage+="\ndata1.fil is not exisited *****From Finally Block's Catch ";
                throw new FileNotFoundException(exceptionMessage);
               }else{
                exceptionMessage+="\nIO exception*****From Finally Block Catch";
                throw new IOException(exceptionMessage);
               }
           }  
            System.out.println("readFile():: Finally Block Ends");
          }
       }
    }
    .
    Last edited by ouou; 05-20-2011 at 08:10 PM. Reason: code tags added

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    I've edited your post to add [code] [/code] tags, but the pasted code is not formatted so they don't help. Please edit your post above and replace the unformatted code with well-formatted code so that others can read it without eye strain and with improved understanding.

  3. #3
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Exception class has a constructor that takes a String parameter. You can add as much information to that String as you like. So you can catch your exception then rethrow your own exception with all the info that you need.

  4. #4
    ouou is offline Member
    Join Date
    May 2011
    Posts
    4
    Rep Power
    0

    Default

    The problem is that "br1.close" in "finally" block could possibly generate an IOException, which overwrite the FileNotException generated from "try block" . Hence, lost the real reason. However, I am required to put br1.close in finally block, now i don't know how to solve my problem, to get the exact causes.

    Quote Originally Posted by Junky View Post
    Exception class has a constructor that takes a String parameter. You can add as much information to that String as you like. So you can catch your exception then rethrow your own exception with all the info that you need.

  5. #5
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    If the FNFE is generated when trying to create the BufferedReader object then there will be no BufferedReader object to close.

  6. #6
    ouou is offline Member
    Join Date
    May 2011
    Posts
    4
    Rep Power
    0

    Default

    Thank you, My question is that before the finally block, there might be a FNFE, what if inside the finally block, there is IOexception happens, it could be overwrite the FNFE. I want to clearly know in the finally block, if there is IOException occurs, I want it keep the original FNFE, not loose it. Can you give me any thoughts on that?thanks again.
    Quote Originally Posted by Junky View Post
    If the FNFE is generated when trying to create the BufferedReader object then there will be no BufferedReader object to close.

  7. #7
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Personally I would not be daisy-chaining exceptions together. A better solution would be to log each exception as they occur.

    If you really must do this then you can save the exception to a local variable to use in the finally clause.

    Java Code:
    } catch (IOException ioe){
        if(ioe.getCause() instanceof FileNotFoundException);
    First there is a semantic error in the above code.
    Second do not do this. Catch the specific exception.

  8. #8
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,006
    Rep Power
    20

    Default

    The basic answer is, don't throw exceptions from a finally block.
    Log them, but don't throw them.

    And the reason for this is, as you've discovered, the finally exception will result in the original exception being hidden.
    The original exception is the important bit, the stuff in a finally block should simply involve tidying up (releasing resources), something which, should it fail, should not result in a failure of that particular method. After all, it's a finally block, so the method should have finished doing what it needs to do by this point.
    ouou likes this.

  9. #9
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,006
    Rep Power
    20

    Default

    Also, to echo Junky (to some extent):
    Java Code:
        	 if(ioe.getCause() instanceof FileNotFoundException);
        	  throw new FileNotFoundException("data1.fil is not exisited");
    Do not catch an exception and then throw a new one without chaining the source exception to it. You lose so much information (including the original stack trace) that you will curse yourself for doing this should you then try and find out where the exception came from. I have had to deal with code that does this and believe me it is evil.

Similar Threads

  1. Print image and send additional PCL instructions.
    By bronai in forum Advanced Java
    Replies: 1
    Last Post: 05-16-2011, 07:18 PM
  2. Exception in object deserialization
    By hanx in forum Advanced Java
    Replies: 3
    Last Post: 03-01-2011, 07:13 PM
  3. Printing Object Information
    By dom12 in forum New To Java
    Replies: 10
    Last Post: 11-04-2010, 06:39 PM
  4. JFrame hangs tried additional threading but no use
    By ravjot28 in forum AWT / Swing
    Replies: 6
    Last Post: 06-21-2010, 06:01 AM
  5. Additional Weight
    By laserjim in forum Lucene
    Replies: 0
    Last Post: 12-13-2009, 01:22 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •