Exception handling in java, understanding on exception handling

How to implement exception handling using java, an article with coding example


Category: Java J2SE Tags: Java J2SE


Exception handling:

     I am going to explain about exception handling. we will discuss exception handling with java. But first we have to know about errors.Types of errors:

  1. Compile time errors (Syntactic errors during the writing of program, like missing semicolon).
  2. Run time errors (happens on run-time, usually programmer do not accept that, like termination of Program).

Exception:- if errors occur at run-time, its effect will be program termination, unexpected result etc. A program won't give any exception if it be use like the way it designed for, if user use it not in standard way as u made it for , it may give exception.

For example:- if u made a program to enter two integer by user for divide them and give the results back to user, if user gives float or division of integer would be a float number so it will give exceptions on runtime.



Exception handling:- Exception handing is prevention technique where you can prevent your code
to be terminate or dealing with errors at runtime.
Exception guaranty that your program will never be trminate causes of exception.


Exception handling mechanism:- We use three blocks here
  1. try
  2. catch
  3. finally
Ex:-

try
{
//code which may generate or throw exception
}
catch
{
//code for handling exception thrown by try block
}
finally
{
//optional part. it executes at last in both condition(exception occur or not)
}
So it is clear that how to use exception handling. which code may generate error we keep that in try and code use for handle exception keeps in catch. catch-block catch the exception thrown by try-block.
And finally is the code which u want to execute in last and in both condition, finally execute in any condition, either exception occur or not.

Let's see exception handling with a code:

import java.io.*;
public class exception_handling_demo
{
public static void main(String args[])
{
PrintStream cout=System.out;
try
{
DataInputStream input;
input=new DataInputStream(System.in);
int n1,n2,ans=0;
cout.println("enter first number:");
n1=Integer.parseInt(input.readLine());
cout.println("enter the second number:");
n2=Integer.parseInt(input.readLine());
ans=n1/n2;
cout.println("division is "+ans);
}
catch(ArithmeticException E)
{
cout.println("exception throwen:\n");
cout.println(E);
}
catch(NumberFormatException E)
{
cout.println("exception throwen:\n");
cout.println(E);
}
catch(IOException E)
{
cout.println("exception throwen:\n");
cout.println(E);
}
finally
{
cout.println("finally block");
}
}
}

Explanation:- here we made a simple program a class named class exception_handling_demo and in main, we wrote a code in try block of handle exception.we have two integer variable to divide them and give answer to user, here we are taking the number from user input. User can give two integers for see result of their division.

We have written that code in try and we used catch with different types of arguments to catch different type exception separately and different function can be performed on different types of exceptions.


Now the important thing that how the exception be handled and how many different
exception may occur here.

When we execute the code and gives the input the output will be in following manner:

OUTPUT:

case1: 
enter first number:
12
enter the second number:
5
division is 2
finally block


case2:
enter first number:
12
enter the second number:
0
exception throwen:

java.lang.ArithmeticException: / by zero
finally block


case3:
enter first number:
45
enter the second number:
4.5
exception throwen:

java.lang.NumberFormatException: For input string: "4.5"
finally block


Explanation of output:


        *Here in case first when inputs are 12 and 5 there no exception generated.(try and finally execute).

        *In second case when we gives the second input zero there, the divide by zero exception occur(try, catch and finally all execute).

        *In third when we enter second input as 4.5 which is a float but we are using integer

there in program as n2, so it will give the exception NumberFormateException(try, catch finally occur).

So we can see that the all of case we saw, happened at runtime, and we have to use

exception handling to dealing with exceptions.



Some-Types of exceptions:


1)Arithmetic Exception -very common exception which occurs when there is any error in the

arithemetic operation u specify.


2)NullPointer Exception - happens if u specify a wrong or unavailable destination.


3)ArrayIndexOutOfBounds Exception - happens if u point the unavailable index of the array.


4)NegativeArraySizeException -happens when u specify negative size for array.


5)NumberFormat Exception -happens in the case of wrong number format.




Understanding with image:



  




Like 1 Person
Last modified on 11 October 2018
Nikhil Joshi

Nikhil Joshi
Ceo & Founder at Dotnetlovers
Atricles: 132
Questions: 9
Given Best Solutions: 9 *

Comments:

No Comments Yet

You are not loggedin, please login or signup to add comments:

Existing User

Login via:

New User



x