Announcements‎ > ‎

Exception Handling

posted Nov 22, 2011, 7:42 PM by Sukhjit Singh
Example covered in class

class FileSizeEx extends Exception {
    FileSizeEx() { }
    //can add remedy methods here.
    //can add exception tracing methods ..written to a file/db.
    void fixtheproblem() {
        try {
            Test1.openfile("abc.txt");
        }
        catch(Exception e)
        {
            System.out.println("We have really gone too far.");
        }
    }
}


import corejava.*;
import java.io.*;

class Test1 {
    static void openfile(String a) throws FileNotFoundException, FileSizeEx
    {

        if (a.length() > 10)
            throw new FileSizeEx();
        try
        {
            FileReader f1 = new FileReader(a);
            System.out.println("Opened file");

        }
        catch(FileNotFoundException e1)
        {
            System.out.println("Wrong FileName");
            a = corejava.Console.readLine("
Enter file name");           
        }

    }

    public static void main(String [] args)
    {
        String a1 = new String(args[0]);
        try {
            openfile(a1);
        }
        catch (FileSizeEx e1)
        {
            System.out.println("Wrong file size");
            e1.fixtheproblem();
        }
        catch (FileNotFoundException e1)
        {
            System.out.println("Wrong file name");
        }
        System.out.println("Yeah!!");
       
       
    }
}
ć
Sukhjit Singh,
Nov 22, 2011, 7:42 PM
Comments