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

Exception Handling Part 1

The document provides examples of exception handling in Java, demonstrating various scenarios such as division by zero and array index out of bounds. It includes code snippets that show the use of try-catch blocks to manage exceptions and print error messages. Additionally, it illustrates how to handle specific exceptions and the use of sleep in a loop with potential exceptions.

Uploaded by

Akash Yadav
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 views16 pages

Exception Handling Part 1

The document provides examples of exception handling in Java, demonstrating various scenarios such as division by zero and array index out of bounds. It includes code snippets that show the use of try-catch blocks to manage exceptions and print error messages. Additionally, it illustrates how to handle specific exceptions and the use of sleep in a loop with potential exceptions.

Uploaded by

Akash Yadav
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/ 16

Lesson 23 Exception Handling part-1:

class A
{
public static void main(String args[])throws Exception
{
int x,y,z;
x=10;
y=2;
z=x/y;

System.out.println(z);//5
System.out.println("Program continues..");
}
}=======================

class A
{
public static void main(String args[])throws Exception
{
int x,y,z;
x=10;
y=0;
z=x/y;

System.out.println(z);
System.out.println("Program continues..");
}
}

class A
{
public static void main(String args[])throws Exception
{
int x,y,z;
x=10;
y=0;
try{
z=x/y;
System.out.println(z);
}
catch(Exception e)
{
System.out.println("Its not possible..");
}
System.out.println("Program continues..");
}
}

class A
{
public static void main(String args[])throws Exception
{
int x,y,z;
x=10;
y=0;
try{
z=x/y;
System.out.println(z);
}
catch(Exception e)
{
System.out.println("Its not possible.."+e);
}
System.out.println("Program continues..");
}
}

class A
{
public static void main(String args[])throws Exception
{
int x,y,z;
x=10;
y=2;
try{
z=x/y;
System.out.println(z);
}
catch(Exception e)
{
System.out.println("Its not possible.."+e);
}
System.out.println("Program continues..");
}
}

class A
{
public static void main(String args[])
{
int x,y,z;
x=10;
y=0;
try{
z=x/y;
System.out.println(z);
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
System.out.println("Program continues..");
}
}

class A
{
public static void main(String args[])
{
int x,y,z;
x=10;
y=0;
try{
z=x/y;
System.out.println(z);
}
catch(Exception e)
{
e.printStackTrace();
}
System.out.println("Program continues..");
}
}

class A
{
public static void main(String args[])
{
int x[]={10,20,30};
try{
System.out.println(x[4]);
}
catch(ArrayIndexOutOfBoundsException e)
{
e.printStackTrace();
}
System.out.println("Program continues..");
}
}

class A
{
public static void main(String args[])
{
int x[]={10,20,30};
try{
System.out.println(x[4]);
}
catch(ArithmeticException e)
{
System.out.println("arithmetic Exception..");
}
catch(NullPointerException e)
{
System.out.println("null pointer exception");
}
catch(IllegalArgumentException e)
{
System.out.println("IllegalAccessError");
}
System.out.println("Program continues..");
}
}

class A
{
public static void main(String args[])
{
int x[]={10,20,30};
try{
System.out.println(x[4]);
}
catch(ArithmeticException e)
{
System.out.println("arithmetic Exception..");
}
catch(NullPointerException e)
{
System.out.println("null pointer exception");
}
catch(IllegalArgumentException e)
{
System.out.println("IllegalAccessError");
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println("My Exception..."+e);
}
System.out.println("Program continues..");
}
}

class B
{
public void hello()
{
for(int i=1;i<=10;i++)
{
if(i==5)
{
Thread.sleep(5000);
}
System.out.println(i);
}
}
}
class A
{
public static void main(String args[])
{
B bo=new B();
bo.hello();
}
}

class B
{
public void hello()throws Exception
{
for(int i=1;i<=10;i++)
{
if(i==5)
{
Thread.sleep(5000);
}
System.out.println(i);
}
}
}
class A
{
public static void main(String args[])throws Exception
{
B bo=new B();
bo.hello();
}
}

class A
{
public static void main(String args[])throws Exception
{
int x[]={10,20,30};
System.out.println(x[3]);
}
}

You might also like