Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
233 views

JAVA Experiment No 5 - Exception Handling in Java

The document describes 10 programs that demonstrate different aspects of exception handling in Java. Program 1 shows an ArithmeticException from integer division by zero. Program 2 shows a NumberFormatException from parsing a non-numeric string as an integer. Program 3 shows an ArrayIndexOutOfBoundsException from accessing an array beyond its bounds. The remaining programs demonstrate try/catch blocks, multiple catch blocks, the throw keyword, nested try blocks, and implementing a user-defined exception.

Uploaded by

Akanksha Thakur
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
233 views

JAVA Experiment No 5 - Exception Handling in Java

The document describes 10 programs that demonstrate different aspects of exception handling in Java. Program 1 shows an ArithmeticException from integer division by zero. Program 2 shows a NumberFormatException from parsing a non-numeric string as an integer. Program 3 shows an ArrayIndexOutOfBoundsException from accessing an array beyond its bounds. The remaining programs demonstrate try/catch blocks, multiple catch blocks, the throw keyword, nested try blocks, and implementing a user-defined exception.

Uploaded by

Akanksha Thakur
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

Experiment no 5

Exception Handling in Java

Program 1: Write a program to show ArithmeticException.


Theoretical Description:

PROGRAM:
public class Main {
public static void main(String[] args) {
int a = 3, b = 0;
try {
int c = a / b;
} catch(ArithmeticException e) {
System.out.println("Exception Caught");
System.out.println("Arithmetic Exception");
}
}
}
OUTPUT:
Exception Caught
Arithmetic Exception
Program 2: Write a program to show NumberFormatException.
Theoretical Description:

PROGRAM:
public class Main {
public static void main(String[] args) {
String s="ABC";
try {
int c = Integer.parseInt(s);
} catch(NumberFormatException e) {
System.out.println("Exception Caught");
System.out.println("Number Format Incorrect!");
}
}
}
OUTPUT:
Exception Caught
Number Format Incorrect!
Program 3: Write a program to show ArrayIndexOutOfBoundsException.
Theoretical Description:

PROGRAM:
public class Main {
public static void main(String[] args) {
try {
int[] a = new int[7];
a[9]=7;
} catch(ArrayIndexOutOfBoundsException e) {
System.out.println("Array Index Out Of Bound!");
}
}
}
OUTPUT:
Array Index Out Of Bound!
Program 4: StringIndexOutOfBound Exception.

Theoretical Description:

PROGRAM:
class ExceptionDemo4
{
public static void main(String args[])
{
try{
String str="beginnersbook";
System.out.println(str.length());;
char c = str.charAt(0);
c = str.charAt(40);
System.out.println(c);
}catch(StringIndexOutOfBoundsException e){
System.out.println("StringIndexOutOfBoundsException!!");
}
}
}

Output:

13
StringIndexOutOfBoundsException!!
Program 5: NullPointer Exception.

Theoretical Description:

PROGRAM:
class ExceptionNP
{
public static void main(String args[])
{
try{
String str=null;
System.out.println (str.length());
}
catch(NullPointerException e){
System.out.println("NullPointerException..");
}
}
}

Output:

NullPointerException..
Program 6: Write a program to show try catch blocks.
Theoretical Description:

PROGRAM:
import java.io.*;
class Example {
public static void main(String args[]) {
FileInputStream fis = null;
try{
fis = new FileInputStream("B:/myfile.txt");
}catch(FileNotFoundException fnfe){
System.out.println("The specified file is not " +
"present at the given path");
}
int k;
try{
while(( k = fis.read() ) != -1)
{
System.out.print((char)k);
}
fis.close();
}catch(IOException ioe){
System.out.println("I/O error occurred: "+ioe);
}
}
}
Output:
This program will display the file contents
Program 7: Write a program to show multiple catch blocks.
Theoretical Description:

PROGRAM:
public class Main {
public static void main(String[] args) {
try {
int[] c = new int[7];
c[7]=4;
} catch(ArithmeticException e) {
System.out.println("Arithmetic Exception Never Caught");
} catch(ArrayIndexOutOfBoundsException e) {
System.out.println("Only Array Index out of Bounds Exception Caught");
}
}
}
OUTPUT:
Only Array Index out of Bounds Exception Caught
Program 8: Write a program to show usage of throw keyword.
Theoretical Description:

PROGRAM:
public class Main {
public static void main(String[] args) {
try {
int[] a = new int[10];
throw new ArrayIndexOutOfBoundsException("Exception Thrown");
} catch(ArrayIndexOutOfBoundsException e) {
System.out.println(e);
System.out.println("Exception Caught");
}
}
}
OUTPUT:
java.lang.ArrayIndexOutOfBoundsException: Exception Thrown
Exception Caught
Program 9: Write a program to show nested try blocks.
Theoretical Description:
PROGRAM:
public class Main {
public static void main(String[] args) {
try {
System.out.println("Outer try started.");
try {
System.out.println("Inner try started.");
int a = 7 / 0;
} catch(Exception e) {
System.out.println("Inner catch printed as exception thrown from inner
try.");
} finally {
System.out.println("Inner finally always printed.");
}
} catch(ArithmeticException e) {
System.out.println("Outer catch never printed as no exception was thrown by
outer block.");
} finally {
System.out.println("Outer finally always printed.");
}
}
}
OUTPUT:
Outer try started.
Inner try started.
Inner catch printed as exception thrown from inner try.
Inner finally always printed.
Outer finally always printed.
Program 10: Write a program to implement user defined exception.
Theoretical Description:
PROGRAM:
class InvalidProductException extends Exception {
public InvalidProductException(String s) {
// Call constructor of parent Exception
super(s);
}
}
public class Example1{
void productCheck(int weight) throws InvalidProductException{
if(weight<100){
throw new InvalidProductException("Product Invalid");
}
}

public static void main(String args[])


{
Example1 obj = new Example1();
try
{
obj.productCheck(60);
}
catch (InvalidProductException ex)
{
System.out.println("Caught the exception");
System.out.println(ex.getMessage());
}
}
}

Output:

Caught the exception


Product Invalid

You might also like