0% found this document useful (0 votes)
10 views

7 - Exception Handling Program

The document contains 3 Java programs that each demonstrate a different exception: ArithmeticException from dividing by zero, NumberFormatException from parsing a non-numeric string as an integer, and ArrayIndexOutOfBoundsException from accessing an array element outside the array bounds.

Uploaded by

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

7 - Exception Handling Program

The document contains 3 Java programs that each demonstrate a different exception: ArithmeticException from dividing by zero, NumberFormatException from parsing a non-numeric string as an integer, and ArrayIndexOutOfBoundsException from accessing an array element outside the array bounds.

Uploaded by

PRAKASH TG NAGAI
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

7 ) A.

// Java program to demonstrate Arithmetic Exception


import java.io.*;

import java.util.*;

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
Can't divide a number by 0
7) b) Java program to demonstrate Number Format Exception

import java.io.*;

import java.util.*;

class NumberFormat_Demo
{
public static void main(String args[])
{
try {
// "akki" is not a number
int num = Integer.parseInt ("akki") ;

System.out.println(num);
}
catch(NumberFormatException e) {
System.out.println("Number format exception");
}
}
}
Output
Number format exception
7) c) Java program to demonstrate Array Index Out Of Bound
Exception
import java.io.*;

import java.util.*;

class ArrayIndexOutOfBound_Demo
{
public static void main(String args[])
{
try{
int a[] = new int[5];
a[6] = 9; // accessing 7th element in an array of
// size 5
}
catch(ArrayIndexOutOfBoundsException e){
System.out.println ("Array Index is Out Of Bounds");
}
}
}
Output
Array Index is Out Of Bounds

You might also like