Wednesday, November 30, 2016

Exception handling simple example with custom exception

Simple example to practice throwing, catching, and creating custom exceptions.  Look at the API documentation for each class and familiarize yourself with the exceptions the class or method can throw in order to pan for exception handling.  In this example, look up FileReader(String ) constructor and take not of the exception and BufferedReader's readLine() method. 

In order to understand exception handling, you need to understand polymorphic object references. In this case, the relevant inheritance tree is as follows:
java.lang.Object

     java.lang.Throwable
         java.lang.Exception
              java.io.IOException
                   java.io.FileNotFoundException 

The program UML is as follows:

Source code that you should copy/paste and experiment with

import java.io.IOException; // For File class and FileNotFoundException
import java.io.File;
import java.util.Scanner; // For the Scanner class 

/**
 * This program demonstrates how a FileNotFoundException, IOException, and custom exception 
 * exception can be handled.
 */
public class OpenFile {
   public static void main(String[] args)   {
     FilePrinter my=new FilePrinter();
     try{
           my.displayFile("c:\\temp\\input.txt");
    }
    catch(IOException e){
            System.out.println("Got you - can't read anymore");   //change source file security rights to deny read.
           }
   }
}

import java.io.*;   
   
public class FilePrinter{   
     private FileReader reader;   
     private BufferedReader inputFile;   
     private String input;   
   
     public void displayFile(String name)throws IOException{   
       //  try{
                 callMe(name);   
       // }
      //  catch(FileNotFoundException e){
      //      System.out.println("File not found");   
      //  }
     }   
         /**
          * This is just a test
          * @param name as a string of the file name including path to the file if any
          * @throws IOException if file was not found
          * @throws FileNotFoundException if file was not able to open
          */

     public void callMe(String name)throws IOException,FileNotFoundException{   
         try{
          callMeAgain(name);   
        }
        catch(MyError e){
            System.out.println(e.toString());
            System.out.println(e.getMessage());
        }
         catch(FileNotFoundException e){
            System.out.println("File name does not exists.");
           // e.printStackTrace();
           }

         }   
          /**
          * This is just a test
          * @param name as a string of the file name including path to the file if any
          * @throws IOException if file was not found
          * @throws FileNotFoundException if file was not able to open
          * @throws MyError if a particular file name was attempted to open
          */

    public void callMeAgain(String name)throws IOException,FileNotFoundException,MyError{   
          if(name.equals("c:\\temp\\two.jpg"))
              throw new MyError();
          if(name.equals("c:\\temp\\two2.jpg"))
              throw new MyError("This is just a custom message!!!");
          try{
                reader = new FileReader(name);                //throws FileNotFoundException
                inputFile = new BufferedReader(reader);       

                input = inputFile.readLine();                    //throws IOException
                while (input != null) {   
                        System.out.println(input);   
                        input = inputFile.readLine();   
                       }   
                inputFile.close();   
               }
        catch(IOException e){
               System.out.println("read not allowed");   
               }
     }   
}   


public class MyError extends Exception
{
public MyError(){
    super("My error - zoltan");
}
public MyError(String msg){
    super(msg);
}
}

No comments:

Post a Comment