next


Java Online Compiler
ArithmeticException Program(Single Exception)
Exception

import java.util.*;
class Arithmetic
{
public static void main(String args[])
{
int x,y,z;
System.out.println("enter x,y values");

Scanner s=new Scanner (System.in);
x=s.nextInt();
y=s.nextInt();
try
{
z=x/y;
System.out.println("division of two number is"+z);
}
catch(ArithmeticException e)
{
System.out.println("Denominator should not be zero");
}
}
}


Multiple Exceptions

import java.util.*;
class Arithmetic
{
public static void main(String args[])
{
int x,y,z;
System.out.println("enter x,y values");

Scanner s=new Scanner (System.in);
x=s.nextInt();
y=s.nextInt();
try
{
z=x/y;
System.out.println("division of two number is"+z);
}
catch(ArithmeticException e)
{
System.out.println("Denominator should not be zero");
}
catch(ArrayIndexOutofBoundException ee)
{
System.out.println("your index is out of range");
}
}
}

Handling Of Unknown Exception using Exception Class

class Except
{
public static void main(String args[])
{
int x,y,z;
try
{
x=Integer.parseInt(args[0]);
y=Integer.parseInt(args[1]);
z=x+y;
System.out.println("z="+z);
}
catch(Exception e)
{
System.out.print(e);
}
}
}

Handling Of Unknown Exception using PrintStackTrace method


class Except
{
public static void main(String args[])
{
int x,y,z;
try
{
x=Integer.parseInt(args[0]);
y=Integer.parseInt(args[1]);
z=x+y;
System.out.println("z="+z);
}
catch(Exception e)
{
e.printStackTrace();
}
}
}

Handling Of Unknown Exception using getMessage method


class Except
{
public static void main(String args[])
{
int x,y,z;
try
{
x=Integer.parseInt(args[0]);
y=Integer.parseInt(args[1]);
z=x+y;
System.out.println("z="+z);
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
}

Throws keyword program


class Demo
{
void f1() throws ArrayIndexOutOfBoundsException,StringIndexOutOfBoundsException
{
String v="hello";
char c=v.charAt(0);
int a[]=new int[5];
a[15]=20;
System.out.println("a[15]="+a[15]);
}
void f2() throws ArithmeticException
{
int x=10,y=0,z;
z=x/y;
System.out.println("z="+z);
}
}
class Demo1
{
void f4(String s1,String s2) throws Exception
{
int f1=Integer.parseInt(s1);
int f2=Integer.parseInt(s2);
int f3=f1+f2;
System.out.println("f3="+f3);
}
}
class Except
{
public static void main(String args[])
{
Demo d=new Demo();
Demo1 dm=new Demo1();
try
{
d.f1();
d.f2();
dm.f4("ten","20");
}
catch(StringIndexOutOfBoundsException se)
{
System.out.println("String index is outof range");
}

catch(ArrayIndexOutOfBoundsException ee)
{
System.out.println("array index is outof range");
}

catch(ArithmeticException ae)
{
System.out.println("String index is outof range");
}
catch(NumberFormatException ee)
{
System.out.println("please enter integer values only");
}
catch(Exception e)
{
System.out.println(e);
}
}
}

Finally Block.


class Finally
{
public static void main(String args[])
{
int x=10,y=0,z;
try
{
z=x/y;
System.out.println("Z="+z);
//System.out.println("hello");
}
catch(ArithmeticException ae)
{
System.out.println("denominator should not be zero");
}
finally
{
System.out.println("hello");// try block statement
}
System.out.println("welcome to sateesh class");//normal program statemet
}
}

UserDefined Excepetion.


Step 1:
//Save The Program With Nage.java
package studentage;
public class Nage extends Exception
{
public Nage(String s)
{
super(s);
}
};

//Compile the program with javac -d . Nage.java

Step 2:


// Save The program with Verify.java
package verifyage;
import studentage.Nage;
public class Verify
{
public void checkage(int a) throws Nage
{
if(a>0)
{System.out.print("valid age");
}
else
{
Nage n=new Nage("Invalid age");
throw(n);
}
}
}
//Compile the program with javac -d . Verify.java
Step 3:
// Save The Program StudentDemo.java
StudentDemo.java
import studentage.Nage;
import verifyage.Verify;
import java.util.Scanner;
class StudentDemo
{
public static void main(String args[])
{
System.out.print("Enter your age");
Scanner s=new Scanner(System.in);
int ag=s.nextInt();
Verify v=new Verify();
try
{
v.checkage(ag);
System.out.print("your age is "+ag);
}
catch(Nage ne)
{
System.out.println("Age should not be Nagetive");
}
}
}

//Compile the program with javac StudentDemo.java

							
							

Array Program Without Using Exception Handling


class ArrDemo
{
public static void main(String args[])
{
int marks[]=new int[5];

marks[0]=20;
marks[1]=20;
marks[2]=20;
marks[3]=20;
marks[4]=20;
System.out.println(marks[10]);
}
}

Array Program With Using Exception Handling
                      

class ArrDemo
{
public static void main(String args[])
{
int marks[]=new int[5];

marks[0]=20;
marks[1]=20;
marks[2]=20;
marks[3]=20;
marks[4]=20;
try
{
System.out.println(marks[10]);
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("Your Array Index Is OutOf Range");
}
}
}

StringIndexOutOfBoundsException Program


class StrDemo
{
public static void main(String args[])
{
int marks[]=new int[5];
String s="Sateesh";
try
{
System.out.println(s.charAt(20));
}
catch(StringIndexOutOfBoundsException e)
{
System.out.println("Your String Index Is OutOf Range");
}
}
}