[go: up one dir, main page]

0% found this document useful (0 votes)
4 views5 pages

Exercise 6

The document provides a series of Java programs demonstrating exception handling mechanisms, including built-in exceptions like ArithmeticException, NullPointerException, and user-defined exceptions. Each program includes a try-catch block to handle specific exceptions and outputs relevant error messages. The document serves as a practical guide for understanding and implementing exception handling in Java.

Uploaded by

buppalaiah.svist
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views5 pages

Exercise 6

The document provides a series of Java programs demonstrating exception handling mechanisms, including built-in exceptions like ArithmeticException, NullPointerException, and user-defined exceptions. Each program includes a try-catch block to handle specific exceptions and outputs relevant error messages. The document serves as a practical guide for understanding and implementing exception handling in Java.

Uploaded by

buppalaiah.svist
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

l O M oA R c P S D | 3 6 5 7 7 5 2 3

EXERCISE - 6

a) Exception handling mechanism


AIM: To write a JAVA program that describes exception handling mechanism

Usage of Exception Handling:


class trydemo
{
public static void main(String args[])
{
try
{
int a=10,b=0;
int c=a/b;
System.out.println(c);
}
catch(ArithmeticException e)
{
System.out.println(e);
}
System.out.println("After the catch statement");
}
}
OUT-PUT:
java.lang.ArithmeticException: / by zero
After the catch statement

b) Illustrating multiple catch classes

AIM: To write a JAVA program Illustrating Multiple catch clauses


class multitrydemo
{
public static void main(String args[])
{
try
{
int a=10,b=5;
int c=a/b;
int d[]={0,1};
System.out.println(d[10]);
System.out.println(c);
}
catch(ArithmeticException e)
{

JAVA PROGRAMMING LAB


l O M oA R c P S D | 3 6 5 7 7 5 2 3

System.out.println(e);
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println(e);
}
System.out.println("After the catch statement");
}
}
OUT-PUT:
java.lang.ArrayIndexOutOfBoundsException:
10After the catch statement

a) creation of Java Built-in-Exceptions


AIM: To write a JAVA program for creation of Java Built-in Exceptions

(i) Arithmetic exception


class arithmeticdemo
{
public static void main(String args[])
{
try
{
int a = 10, b = 0;
int c = a/b;
System.out.println (c);
}
catch(ArithmeticException e)
{
System.out.println (e);
}}}
OUT-PUT:
java.lang.ArithmeticException: / by zero

JAVA PROGRAMMING LAB


l O M oA R c P S D | 3 6 5 7 7 5 2 3

(ii) NullPointer Exception


class nullpointerdemo
{
public static void main(String args[])
{
try
{
String a = null;
System.out.println(a.charAt(0));
}
catch(NullPointerException e)
{
System.out.println(e);
}
}
}
OUT-PUT:
java.lang.NullPointerException

(iii) StringIndexOutOfBound Exception


class stringbounddemo
{
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(e);
}
}
}
OUT-PUT:
java.lang.StringIndexOutOfBoundsException: String
index out of range: 24

JAVA PROGRAMMING LAB


l O M oA R c P S D | 3 6 5 7 7 5 2 3

(iv) FileNotFound Exception


import java.io.*;
class filenotfounddemo
{
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(e);
}
}
}
OUT-PUT:
java.io.FileNotFoundException: E:\file.txt (The system cannot find the file specified)

(v) NumberFormat Exception


class numberformatdemo
{
public static void main(String args[])
{
try
{
int num = Integer.parseInt ("akki") ;
System.out.println(num);
}
catch(NumberFormatException e)
{
System.out.println(e);
}
}
}
OUT-PUT:
java.lang.NumberFormatException: For input string: "akki"

JAVA PROGRAMMING LAB


l O M oA R c P S D | 3 6 5 7 7 5 2 3

(vi) ArrayIndexOutOfBounds Exception


class arraybounddemo
{
public static void main(String args[])
{
try
{
int a[] = new int[5];
a[6] = 9;
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println (e);
}
}
}
OUT-PUT:
java.lang.ArrayIndexOutOfBoundsException: 6

b) creation of User Defined Exception


AIM: To write a JAVA program for creation of User Defined Exception

class A extends Exception


{
A(String s1)
{
super(s1);
}}
class owndemo
{
public static void main(String args[])
{
try
{
throw new A("demo ");
}
catch(Exception e)
{
System.out.println(e);
}
}
}
OUT-PUT:
A: demo

JAVA PROGRAMMING LAB

You might also like