JP-3
Practical 3A
Write a Java Program to raise built in exception such as
> Arithmetic exception
Code:
class ArithmeticException_Demo
{
public static void main(String args[])
{
try
{
int a = 30, b = 0;
int c = a / b; // cannot divide by zero
System.out.println("Result = " + c);
}
catch (ArithmeticException e)
{
System.out.println("Can't divide a number by 0");
}
}
}
Output:
> Arrayindexoutofbounds exception
Code:
class ArrayIndexOutOfBound_Demo
{
public static void main(String args[])
{
try
{
int a[] = new int[5];
a[6] = 9;
}
catch (ArrayIndexOutOfBoundsException e)
{
System.out.println("Array Index is Out Of Bounds");
}
}
}
Output:
> Classnotfound exception
Code:
public class GFG
{
public static void main(String args[])
{
try
{
Class.forName("GeeksForGeeks");
}
catch (ClassNotFoundException ex)
{
ex.printStackTrace();
}
}
}
Output:
> Filenotfound exception
Code:
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
class File_notFound_Demo
{
public static void main(String args[])
{
try
{
File file = new File("E:// file.txt");
FileReader fr = new FileReader(file);
}
catch (FileNotFoundException e)
{
System.out.println("File does not exist");
}
}
}
Output:
> IO exception
Code:
import java.io.*;
class Geeks
{
public static void main(String args[])
{
try
{
FileInputStream f = null;
f = new FileInputStream("abc.txt");
int i;
while ((i = f.read()) != -1)
{
System.out.print((char)i);
}
f.close();
}
catch (IOException e)
{
System.out.println("IO Exception");
}
}
}
Output:
> Nullpointer exception
Code:
class NullPointer_Demo
{
public static void main(String args[])
{
try
{
String a = null; // null value
System.out.println(a.charAt(0));
}
catch (NullPointerException e)
{
System.out.println("NullPointerException..");
}
}
}
Output:
> Numberformat exception
Code:
class NumberFormat_Demo
{
public static void main(String args[])
{
try
{
int num = Integer.parseInt("akki");
System.out.println(num);
}
catch (NumberFormatException e)
{
System.out.println("Number format exception");
}
}
}
Output:
> Stringindexoutofbounds exception
Code:
class StringIndexOutOfBound_Demo
{
public static void main(String args[])
{
try
{
String a = "This is like chipping ";
char c = a.charAt(24);
System.out.println(c);
}
catch (StringIndexOutOfBoundsException e)
{
System.out.println("StringIndexOutOfBoundsException");
}
}
}
Output:
PRACTICAL 3B
Write a java program to define user defined exception
Code:
class MyException extends Exception
{
public MyException(String s)
{
super(s);
}
}
public class Main
{
public static void main(String args[])
{
try
{
throw new MyException("Java Practical");
}
catch (MyException ex)
{
System.out.println("Caught User Defined Exception");
System.out.println(ex.getMessage());
}
}
}
Output:
Code:
class MyException extends Exception
{
}
public class setText
{
public static void main(String args[])
{
try
{
throw new MyException();
}
catch (MyException ex)
{
System.out.println("Caught Exception");
System.out.println(ex.getMessage());
}
}
}
Output:
Comments
Post a Comment