Object Oriented Programming-I (LAB Session :- 8)


 

JAVA Programming to handle Exceptions


1.  Write a Java program to generate any eight Exception (Checked or Un-checked)  and catch those          exceptions in a catch block written specifically to catch only  that exception. Print a message in catch block that explains user why that  exception occurred and also print the Stack Trace for the exception.

CODE :

import java.io.*;

public class Exceptiontype {

    public static void main(String[] args) {

        

        try{

            int a[] = new int[5];

            a[6] = 5

        }

        catch(ArrayIndexOutOfBoundsException e){

            System.out.println (e.getMessage() + "\n" + e.getCause() + "\n" + e.getStackTrace());

        }

        

        try { 

            int a = 30, b = 0

            int c = a / b; 

            System.out.println("Result = " + c); 

        }        catch (ArithmeticException e) { 

            System.out.println("\n"+e.getMessage() + "\n" + e.getCause() + "\n" + e.getStackTrace()); 

        } 

        

       try { 

            File file = new File("E:// file.txt"); 

            FileReader fr = new FileReader(file); 

        }         catch (FileNotFoundException e) { 

            System.out.println("\n"+e.getMessage() + "\n" + e.getCause() + "\n" + e.getStackTrace()); 

        } 

        

        try { 

            String a = null;  

            System.out.println(a.charAt(0)); 

        }        catch (NullPointerException e) { 

            System.out.println("\n"+e.getMessage() + "\n" + e.getCause() + "\n" + e.getStackTrace()); 

        } 

        

        try { 

            int num = Integer.parseInt("akki"); 

            System.out.println(num); 

        }         catch (NumberFormatException e) { 

            System.out.println("\n"+e.getMessage() + "\n" + e.getCause() + "\n" + e.getStackTrace()); 

        }      

        try { 

            String a = "This is like chipping "

            char c = a.charAt(25);

            System.out.println(c); 

        }   catch (StringIndexOutOfBoundsException e) { 

            System.out.println("\n"+e.getMessage() + "\n" + e.getCause() + "\n" + e.getStackTrace()); 

        }         

    }

}

OUTPUT :



2.Write a java program to read a text file from a particular folder. Handle the FileNotFoundException in catch block. Run the program for below cases.

a.The file is present in the folder. In this case, your code should read

the lines written in the file and print the output.

CODE :

import java.io.File; 

import java.io.FileNotFoundException; 

import java.util.Scanner; 

 

public class Exception { 

 

    public static void main(String[] args) throws FileNotFoundException  {         

      FileReader fr = new FileReader(); 

      fr.readFile();         

    }      

class FileReader { 

    void readFile() throws FileNotFoundException

        File myobj = null;        

        Scanner myReader = null

        myobj = new File("D:\\OopI\\lab8a.txt"); 

        myReader = new Scanner (myobj);        

        

      while (myReader.hasNextLine()){          

            String data = myReader.nextLine(); 

            System.out.println(data); 

        } 

    } 

}

OUTPUT : 


b.The file is NOT present in the path specified. In this case, the code should handle the exception and create a new text file in the specified path. This code should be written in the catch block of your code.

CODE :

import java.io.File; 

import java.io.FileNotFoundException; 

import java.util.Scanner; 

import java.io.IOException; 

import java.util.logging.Level; 

import java.util.logging.Logger;      

 

public class Exception1 { 

 

    public static void main(String[] args) throws FileNotFoundException  { 

 

      FileReader fr = new FileReader();      

   try {           

               fr.readFile(); 

 

        } catch (IOException ex) { 

 

            Logger.getLogger(Exception.class.getName()).log(Level.SEVERE, null, ex); 

        } 

     }      

 

class FileReader { 

 

    void readFile() throws IOException { 

 

        File myobj = null

        Scanner myReader = null

 

        try

 

        myobj = new File("D:\\oopI\\lab8.txt");         myReader = new Scanner (myobj);         while (myReader.hasNextLine()){ 

 

            String data = myReader.nextLine(); 

 

            System.out.println(data); 

        } 

   } 

     catch(FileNotFoundException e){ 

 

            System.out.println("this file is not present at this path...");             if(!myobj.exists()){                 myobj.createNewFile(); 

            } 

 

            System.out.println("you have created a new file at your given location"); 

 

            }         finally

 

            if(myReader != null)            

               myReader.close(); 

        } 

    } 

}

OUTPUT :



DOWNLOAD PDF HERE.....



No comments:

Post a Comment