Exception Handling in JAVA 2
‘throw’ keyword:-
In above example, exception occurs in implicit manner but our exception explicitly by creating an object or that exception.
For throwing an exception explicitly we have to use a ‘throw’ keyword which throws as an exception towards appropriate catch block.
e.g.
void fun()
{
int i;
try{
System.out.println(“inside a try block.”);
i=10/0;
}catch(ArithmaticException e)
{
System.out.println(“inside the 1st catch block.”);
throw new ArithmaticException(“my exception.”);
}
}
public static void main(String args[])
{
try{
System.out.println(“before calling a function”);
fun();
System.out.println(“after calling a function”);
}
catch(ArithmaticException e)
{
System.out.println(“inside catch of a main”);
// System.out.println(“inside a parameterized constructor”+e);
}
}
Output:-
before calling a function
inside a try block
inside 1st catch block
inside catch of a main
User defined Exception:-
In java, we can create our own user defined class whose object is considered as an Exception object and we can throw that object if there is some exception.
If our class is considered as a user defined exception class then it must be extended from an Exception class.
The syntax and meaning of user defined exception is same as predefined exceptions.
e.g.
class userException extends Exception
{
// code
}
class demo
{
public void fun()
{
int i,j;
i=11;
try{
if(I<200)
throw new userException();
}
catch(userException e)
{
System.out.println(“the user defined Exception”);
}
}
}
In this example, we declared our own user defined exception as userException we throw in normal way.
userException class is extended from the Exception class means we can accept an exception by using exception class object.
In this manner we can create our user defined exception and class which is used for creating user defined exception must provide exception handling code.
‘throws’ keyword:-
When we use throw keyword an exception is explicitly thrown from our class or a function.
throws keyword is used for indicating that our function may throw some exception.
In this case, caller of that function must handle the exception which is thrown from the function.
Inside the function, we can also handle an exception and we can also re-throw an exception.
e.g.
class demo
{
public void fun() throws ArithmaticException
{
int i,j;
i=10;
j=0;
try{
if(j==0)
throw new ArithmaticException();
}
catch(ArithmaticException e)
{
System.out.println(“inside a catch of fun.”);
throw e; // re-throw
}
}
}
class hello
{
public static void main(String args[])
{
try
{
demo d = new demo();
d.fun();
}
catch(ArithmaticException)
{
System.out.println(“inside catch of a main”);
}
}
}
Output:-
inside catch of fun
inside catch of main
finally:-
finally is a block which is executed definitely after handling try and catch blocks.
If there is an exception in our program then our program terminates abnormally and due to this abnormal termination resources which are accepted in our program remain inside address space of our program.
To avoid this problem, we can write a code which releases all the resources which are accepted in our program inside a finally block.
Important point about finally block is is must be executed in try scenario.
Generally the code which used for de allocating resources is returned inside a finally block.
There is no special call for executing finally block.
If we are unable to provide a catch block then we have to provide at least finally block for our try block.
If there is no catch block or finally block for our try block then compiler generate an error.
To avoid that error we can provide finally block.
e.g.
void fun()
{
try{
throw new ArithmaticException();
}
catch(ArithmaticException e)
{
System.out.println(“inside a catch block.”);
}
finally
{
System.out.println(“inside a finally block.”);
}
}
Output:-
inside a catch block
inside a finally block
For more reading about technology news in singapore or from all over the world in all aspect from seo to online marketing do view more about other pages.