Exception Handling
Exception Handling
//EXCEPTION HANDLING
PROGRAM:
import java.util.*;
try{
int data=100;
int divide;
divide=sc.nextInt();
data=data/divide;
}catch(ArithmeticException f){
System.out.println(f);
OUTPUT:
Enter data: 0
java.lang.ArithmeticException: / by zero
Enter data: 5
20
// MULTIPLE CATCH
PROGRAM:
import java.util.*;
try{
int[] a;
a= new int[5];
int x;
int quotient;
x=sc.nextInt();
{ try{
System.out.print("Enter "+(i+1)+"\n");
a[i]=sc.nextInt();
quotient=a[i]/x;
System.out.print(quotient);
//System.out.print(e);
} } }
System.out.print(e);
catch(Exception z)
{
System.out.print("\nAfter using the exception handling, the rest of the code works fine!");
OUTPUT:
12
Enter 2
85
Enter 3
13
Enter 4
Enter 5
After using the exception handling, the rest of the code works fine!
PROGRAM:
class A{
if (age<18){
else{
{ print(3);
OUTPUT:
at A.print(A.java:7)
at A.main(A.java:16)