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

Exception

The document discusses exception handling in Java, explaining the difference between compile-time and runtime errors. It outlines various pre-defined exception classes, such as ArithmeticException and ArrayIndexOutOfBoundsException, and provides examples of how to implement try-catch-finally blocks for handling exceptions. Additionally, it introduces the throw statement for explicitly raising exceptions.

Uploaded by

Pace Infotech
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 views15 pages

Exception

The document discusses exception handling in Java, explaining the difference between compile-time and runtime errors. It outlines various pre-defined exception classes, such as ArithmeticException and ArrayIndexOutOfBoundsException, and provides examples of how to implement try-catch-finally blocks for handling exceptions. Additionally, it introduces the throw statement for explicitly raising exceptions.

Uploaded by

Pace Infotech
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/ 15

Exception Handling :

Exception are the runtime error . If we have not specified a semi-colon , not declared a variables
etc.. then these all errors at being notified the complier , so these are known as the compile time
error
But , if we divide a number by zero, if we access an element in an array outside its bound then an
exception is occurred , and this all will occur at the run-time

Consider the following program


class ExceptionDemo1
{
public static void main(String args[ ])
{

int a=Integer.parseInt(args[0]);
int b=Integer.parseInt(args[1]);

int c;

c=a/b;

System.out.println("c="+c);

}
}

```
Consider the following program ,

class ExceptionDemo2
{
public static void main(String args[ ])
{

a();
}
static void a()
{
b();
}
static void b()
{
c();
}
static void c()
{

int a=15;
int b=0;

int c;

c=a/b;

System.out.println("c="+c);

}
}
In java for each and every exception there is a class .
The super class of all exceptions is Throwable.

Exception class contains details of the exception which occurs under the normal circumstances
Error class contains those exception which doesnot occur under the normal circumstances e.g.
stack overflow . These exception are not handled under normal situation

Pre-Defined Classes of the Exception class

------------------------------------------------------------------------
Class Description
------------------------------------------------------------------------

1. ArithmeticException This class exception occurs when we divide the


number by zero.

2. ArrayIndexOutofBoundsException
This class exception occurs when we refer to an index outside the bounds or its size (0 to size-1).

3.ClassNotFoundException
This class exception occurs when we either create an object or reference of class , which we have
not defined or which neither occurs in any package.

4. StringIndexOutofBoundsException
This class exception occurs when we refer to an index outside the bounds in case of a string .
5. NegativeArraySizeException
This class exception occurs when we specify a negative value as an array size .
etc....
------------------------------------------------------------------------
The general form of Exception Handling :

try
{
//try block
}
catch(ExceptionType1 objectname)
{
//body of catch block
}
catch(ExceptionType2 objectname)
{
//body of catch block
}
::
::
::
finally
{
//body of finally block
}

try block : It contains those statements which are to be examined for exception.

catch block : It is used for handling the exceptions.

finally block : It contains those statements which are always to be executed whether or not an
exception takes place.
class ExceptionDem1
{
public static void main(String args[ ])
{
System.out.println("In the Main block");
try
{
System.out.println("In the try block");
int a=10,b=0,c;

c=a/b;

System.out.println("Result="+c);
}
catch(ArithmeticException ae)
{
System.out.println("Exception :"+ae);
}
System.out.println("Outside the Try block");
}
}

class ExceptionDem2
{
public static void main(String args[ ])
{
System.out.println("In the Main block");
try
{
System.out.println("In the try block");
int a=10,b=0,c;

c=a/b;

System.out.println("Result="+c);
}
finally
{
System.out.println("In the finally block");
}

}
}

class ExceptionDem3
{
public static void main(String args[ ])
{
System.out.println("In the Main block");
try
{
System.out.println("In the try block");
int a=10,b=0,c;

c=a/b;

System.out.println("Result="+c);
}

}
}

class ExceptionDem4
{
public static void main(String args[ ])
{
System.out.println("In the Main block");
try
{
System.out.println("In the try block");

int a=Integer.parseInt(args[0]);
int b=Integer.parseInt(args[1]);
int c;
c=a/b;

System.out.println("Result="+c);
}
catch(ArrayIndexOutOfBoundsException ie)
{
System.out.println("Invalid Index");
}
catch(ArithmeticException ae)
{
System.out.println("Divide By zero");
}
finally
{
System.out.println("Always execute");
}

}
}
class ExeptionDem5
{
public static void main(String args[ ])
{
try
{
int a=10,b=0;

int c=a/b;

System.out.println(“c=” +c);

}
catch(Exception e)
{
System.out.println(“Exception :”+e);
}
catch(ArtithmeticException ae)
{
System.out.println(“Exception :”+ae);
}
}
}

class ExceptionDem6
{
public static void main(String args[ ])
{
try
{
int a=Integer.parseInt(args[0]);
int b=Integer.parseInt(args[1]);

int c=a/b;

System.out.println("c=" +c);

}
catch(ArithmeticException ae)
{
System.out.println("Exception :"+ae);
}

catch(Exception e)
{
System.out.println("Other Exception");
}
}
}

throw statement :
The throw statement is used to explicitly raise the exception .

The general form is

throw new Exceptiontype();


class ExceptionDem7
{
public static void main(String args[ ])
{

try
{

int a,b;

if(args.length!=2)
throw new ArrayIndexOutOfBoundsException();

a=Integer.parseInt(args[0]);
b=Integer.parseInt(args[1]);

int c;

if(b==0)
throw new ArithmeticException();

c=a/b;

System.out.println("Result = "+c);
}
catch(Exception e)
{
System.out.println("Exception Is :"+e);
}
}
}

You might also like