Oops Exception Handling
Oops Exception Handling
Oops Exception Handling
AIM:
To write a java program to use any three unchecked Exceptions.
ALGORITHM:
Step 1: Start the program.
Step 2: Declare a method/objects and place the statements in try block and any three
unchecked exception say, ArithmeticException ,ArrayIndexOutOfBounds,
NullPointerException in catch block.
Step 3: Throw the exception from try block, write a catch block that prints message of
unchecked exception.
Step 4: Stop the program.
PROGRAM:
public class javaprog
{
public static void main(String[] args) {
try{
try
{
int m=10;
int n=0;
int ans=m/n;
System.out.println(ans);
}
catch(ArithmeticException e)
{
System.out.println(e);
}
try
{
int arr[] ={8,2,6,4,5};
System.out.println(arr[9]);
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println(e);
}
try
{
int[] data = null;
System.out.println( data.length);
}
catch(NullPointerException e)
{
System.out.println(e);
}
}
catch(Exception e){
System.out.println("handled");
}
System.out.println("NORMAL FLOW..");
}}
OUTPUT:
RESULT:
For the above program, the output was verified successfully.
EX NO: 5B
I/O EXCEPTIONS
DATE:
AIM:
To create a java program to handle the I/O exceptions.
ALGORITHM:
Step 1: Start the program.
Step 2: Declare a File path.
Step 3: Throw the exception from try block, write a catch block that prints message of
exception.
Step 4: Stop the program.
PROGRAM:
import java.io.File;
import java.io.IOException;
public class javaprog
{
public static void main(String[] args) throws IOException
{
File file = new File("C:\\Users\\Brindha\\Downloads\\eclipse-java\\helo\\file_1.txt");
try
{
boolean f = file.createNewFile();
if ( f )
{
System.out.println("New File is created.");
}
else
{
System.out.println("File exists already.");
}
}
catch (IOException e)
{
System.out.println(e);
}
}
}
OUTPUT:
Sample 1:
Sample 2:
Sample 3:
RESULT:
For the above program, the output was verified successfully.
EX NO: 5C
FILE WITH EXCEPTION HANDLING
DATE:
AIM:
To create a java program to read data from a file with exception handling routines
ALGORITHM:
Step 1: Start the program.
Step 2:
PROGRAM:
import java.io.*;
import java.util.Scanner;
public class javaprog
{
public static void main(String args[]) throws IOException
{
DataInputStream data = null;
try
{
FileInputStream f = new FileInputStream("C:\\Users\\Brindha\\Downloads\\eclipse-
java\\helo\\src\\Computer.txt");
line = data.readLine();
}
}
catch (FileNotFoundException e)
{
System.out.println(e);
}
finally
{
System.out.println("File is Closed..");
}
}
}
OUTPUT:
RESULT:
For the above program, the output was verified successfully.