When a program is developed and execute for the first time, it will most probably have mistakes. While some of them may only result in the generation of wrong output, other may cause unexpected termination of the execution of the program. Some serious mistakes may even cause a system crash. The errors involved in a program may either be completion time errors or run time errors. The completion errors are the syntax errors and they are caught during the completion stage itself. The errors occurs during the execution of a program are known as run time errors . An exception is an Abnormal condition that is caused by a run time errors in the program.
EXCEPTION HANDLING IN JAVA
}
Introduction
The exception in handling in Java is the influential mechanism to handle the run time errors so that usual flow of the application can be maintained. Exceptions are events that occurs during the execution of programs, Such as divided by zero, array access out of bound etc. That interrupt the normal flow of instructions. It is an objective which is thrown at run time. For example.
Statement 1;
Statement 2;
Statement 3;
Statement 4;
Statement 5;// exception occurs
Statement 6;
Statement 7;
Statement 8;
Statement9 ;
Statement 10;
Assume that there are 10 statements in a program and there occurs an exception at statement 5. The rest of the code will not be executed, that is, statements 6 to 10 will not run. If the concept of exception handling is applied to the above case, the rest of the statements will be executed. That is the purpose of using exception handling in Java. This chapter explain how exceptions Can be thrown, caught and handled.
In Java, exceptions are objects, when an exception is thrown it is treated a throwing an object. All types of exception are sub classes of the built in abstract class Java. lang.Throwable . Throwable serves as the base class for an entire family of classes declare in Java. Lang that a program can instantiate and throw. There are two sub classes that partition exceptions into two distinct
EXCEPTION HANDLING IN JAVA
Exception handling in java is a machenism to handle the run time errors. exception is a abnormal condition.
there are many types of exception occures ,such as Arithmetic Exception ,ArrayOutofBoundsException etc.
Types of exception: there are two types of exception
1.checked exception
2.unchecked exception
1.checked exception: this type of exception compulsory to handle . and write in rry and catch blocks.if we don't handle the exception than program forcefully terminate.
2.unchecked. in this section does not compulsory to handle the exception ,if we don't handle exception than program does not forcefully terminate.
some types of exceptions occures follow as_
Arithmetic Exception:
int a =10/0;
ArrayOutOfBounds Exception:
int a=new int[5];
a[5]=50;
Nullpointer Exception:
String name="Null";
Exception handling keywords
1.try
2.catch
3.finally
4.throw
5. throws
1.try block: in the try block exception will be occures .and throw the exception into the catch block.
syntax:
try
{
statements;
}
Example:
class A
{
public static void main(String args[])
{
try
{
int a=10/0;
}
catch(ArithmeticException e)
{
system.out.println(e);
}
}
}
2. catch: when exception will be occures in the try block and throw the exception into catch block than catch block can be handle the exception.we can use multiple catch blocks in java exception handling.
Syntax:
catch(Exception e)
{
statements;
}
Example:
class B
{
public static void main(String args[])
{
try
{
int a=new int a[5];
a[5]=50;
}
catch(ArrayOutOfBoundsException e)
{
System.out.println(e);
}
}
}
we can use multiple catches in java:
syntax:
try
{
statements;
}
catch(ArithmeticException e)
{
statements;
}
catch(Exception e)
{
statements;
}
catch(ArrayOutOfBoundsException e)
{
statements;
}
class Sample00
{
public static void main(String args[])
{
try
{
int arr[]=new int[10];
arr[10]=80;
}
catch(ArithmeticException e)
{
System.out.println(e);
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println(e);
}
catch(Exception e)
{
System.out.println(e);
}
System.out.println("hello java learning concepts");
}
}
output:
3. finally: finally block is a special block .it is always execute if the exception will be handle and not.
try
{
statents;
}
catch(Exception e)
{
statements;
}
finally
{
statements;
}
example:
example:
class Sample00
{
public static void main(String args[])
{
try
{
int arr[]=new int[10];
arr[10]=80;
}
catch(ArithmeticException e)
{
System.out.println(e);
}
catch(ArrayIndexOutOfBoundsException e)
{
System.out.println(e);
}
catch(Exception e)
{
System.out.println(e);
}
finally
{
Syatem.out.println("good morning");
{
Syatem.out.println("good morning");
}
}
output:
4.throw: when exception occures the in the try block and throw the exception throw into the catch block automatically but if we use throw keyword then exception can be throw splicitely .
Arithmetic Exception ob=new Arithmetic Exception();
throw ob ;
Example:
class A
{
public static void main(String args[])
{
try
{
int a=10/0;
ArithmeticException ob=new AirthmeticException();
throw ob;
ArithmeticException ob=new AirthmeticException();
throw ob;
}
catch(ArithmeticException e)
{
system.out.println(e);
}
}
}
5. throws: in this section the exception will be not handle a particular way. the exception will be handle any
way.
Syntax: return type method name() throwsIo Exception .