There are two type error in java. first is handle error (Exception) and second is non handle error.Exception is an error but we can handle it error.there are various way to handle Exception.In this tutorial we will learn about why occur Exception and how can be handle it.
There are some reasons for an exception– Divide by zero,Opening a non-existing file,any null value and index not found in any array etc.
First we will talk about type of exception in java : -there are two type exception occur in java, check exception and other is unchecked exception.
checked Exception:-its occur at compile time so its also called compile time Exception
examples: classNotFoundException, IllegalAccessException, NoSuchFieldException etc.
unchecked Exception:- its occur at run time so its also called run time Exception.
example: NullPointerException, NegativeArraySizeException, ArrayIndexOutOfBoundsException etc.
How to handle Exception;-
In this tutorial we will learn about four way to handle Exception in java.
by the use of throw,throws, try-catch and finally, we will discuss with examples blow.
1. Use of throw:-throw keyword is used to handle exception at run time. lets take a example for handle exception with the help of throw clause.
Example:-
public void class ThrowSample(){
//your statment
void getNumber(int num){
if(num>18)
throw new IOException("age 18+");
}
public static void main(String []args){
getNumber(23);
}
}
2.Use of throws:- throws is keyword for handle cheeked exception by the use of throws keyword.by the use of throws we can declare multiple exception in java.
Example:-
public class ThrowsDemo {
void test(int num)throws IOException, NoSuchFieldException{
if(num==1)
throw new IOException("Exception occur first");
else
throw new NoSuchFieldException("Exception occur second");
}
}
class throwsTest{
public static void main(String args[]){
try{
ThrowsDemo obj=new ThrowsDemo();
obj.mymethod(1);
}catch(Exception ex){
System.out.println(ex);
}
}
}
3.try-catch
The try block is a block for using within java statements which an exception can be occur.catch block always use at the end of try block to handle exception and always required Exception type parameter.we can be use nested try-catch block. .
class Demo{
try{
//your statement may be any Exception cause.
}catch(Create Exception object){
//handle exception here
}
}
4.finally
A finally block always use at the end of the catch blocks and finally block always execute at the end of try block.lets see an example blow
class Demo{
try{
//your statement may be any Exception cause.
}catch(exception object ){
//handle exception here
}
finally{
//statement here its always run
}
}
There are some reasons for an exception– Divide by zero,Opening a non-existing file,any null value and index not found in any array etc.
First we will talk about type of exception in java : -there are two type exception occur in java, check exception and other is unchecked exception.
checked Exception:-its occur at compile time so its also called compile time Exception
examples: classNotFoundException, IllegalAccessException, NoSuchFieldException etc.
unchecked Exception:- its occur at run time so its also called run time Exception.
example: NullPointerException, NegativeArraySizeException, ArrayIndexOutOfBoundsException etc.
How to handle Exception;-
In this tutorial we will learn about four way to handle Exception in java.
by the use of throw,throws, try-catch and finally, we will discuss with examples blow.
1. Use of throw:-throw keyword is used to handle exception at run time. lets take a example for handle exception with the help of throw clause.
Example:-
public void class ThrowSample(){
//your statment
void getNumber(int num){
if(num>18)
throw new IOException("age 18+");
}
public static void main(String []args){
getNumber(23);
}
}
2.Use of throws:- throws is keyword for handle cheeked exception by the use of throws keyword.by the use of throws we can declare multiple exception in java.
Example:-
public class ThrowsDemo {
void test(int num)throws IOException, NoSuchFieldException{
if(num==1)
throw new IOException("Exception occur first");
else
throw new NoSuchFieldException("Exception occur second");
}
}
class throwsTest{
public static void main(String args[]){
try{
ThrowsDemo obj=new ThrowsDemo();
obj.mymethod(1);
}catch(Exception ex){
System.out.println(ex);
}
}
}
3.try-catch
The try block is a block for using within java statements which an exception can be occur.catch block always use at the end of try block to handle exception and always required Exception type parameter.we can be use nested try-catch block. .
class Demo{
try{
//your statement may be any Exception cause.
}catch(Create Exception object){
//handle exception here
}
}
4.finally
A finally block always use at the end of the catch blocks and finally block always execute at the end of try block.lets see an example blow
class Demo{
try{
//your statement may be any Exception cause.
}catch(exception object ){
//handle exception here
}
finally{
//statement here its always run
}
}