How to use Exception handling in java?

Exception are run time anomalies or unusual conditions that a program may encounter while executing. Anomalies might include conditions such as division by zero, access to an array outside of is bounds, or running out of memory or disk space.

Exceptions are of two kinds, namely, synchronous exception and asynchronous exceptions. Errors such as “out of range index” and “over  flow” belong to the synchronous type exceptions. The errors that are caused by events beyond the control of the program are called asynchronous exceptions.

The purpose of the Exception handling is to provide means to detect and report an “exceptional circumstance” so that the appropriate action can be  taken.

Java Exception Handling Keywords

There are 5 keywords used in java exception handling.

  • Try
  • Catch
  • Finally
  • Throw
  • Throws

 

Advantage of Exception Handling

The core advantage of exception handling is to maintain the normal flow of the application. Exception normally disrupts the normal flow of the application that is why we use exception handling. Let’s take a scenario:

Suppose there is 3 statements in your program and there occurs an exception at statement 1st, rest of the code will not be executed i.e. statement 2nd and 3rd will not run. If we perform exception handling, rest of the exception will be executed. That is why we use exception handling in java.

 

Example:-

In this program we use try and catch.

//code

 

class demo

{

public static void main(String arg[])

{

try

{

System.out.println(5/0);

}

catch(Exception e)

{

System.out.println(“U can’t devide by zero”);

}

System.out.println(“Hello india”);

}

}

Step 1: Name it as “excep.java”and save the file in any location i saved at “c:/kiran/program”.

Step 2: Set the paath,without path setting our java file is not compile.1

Step 3: Now write the following code for checking my java file is compiling or not.

javac excep.java

2

My java program compiled successfully.

 

Step 4: Write the following code in command prompt. press enter and see the output.

java demo

// demo is a class name which is written in my “excep.java” file.

//Output

3

 

Step 5: If  we put(5/3) the output is-

4

Step 6: Now, use the Try, Catch and finally.

 

Code//

 

class demo

{

public static void main(String arg[])

{

 

try

{

System.out.println(5/4);

}

catch(Exception e)

{

System.out.println(“U Can’t Divided by Zero”);

}

finally

{

System.out.println(“Hello india”);

}

 

}

}

 

Output:

5

Step 7: If we put (5/0) the output is-

6

Means, Finally block always execute either exception occurs or not.

 

Step 8: Now, we use Try, Catch and Throws.

Code//

 

class MyExcep extends Exception

{

String msg()

{

return “U are not valid”;

}

}

class CoreExcep

{

void Checkage(int age) throws Exception

{

if(age<=18)

{

throw new MyExcep();

}

else

{

System.out.println(“U are valid”);

}

}

public static void main(String arg[]) throws Exception

{

CoreExcep ce=new CoreExcep();

try{

ce.Checkage(20);

}

catch (MyExcep e){

System.out.println(e.msg());}

}

}

7

Step 9: If we put 17 on the place of 20.

 

Code//

class MyExcep extends Exception

{

            String msg()

            {

                        return “U are not valid”;

            }

}

class CoreExcep

{

            void Checkage(int age) throws Exception

            {

            if(age<=18)

            { 

                        throw new MyExcep();

            }          

            else

            {

                        System.out.println(“U are valid”);

            }

}

            public static void main(String arg[]) throws Exception

            {

                        CoreExcep ce=new CoreExcep();

                        try{

                        ce.Checkage(17);

                        }

                        catch (MyExcep e){

                        System.out.println(e.msg());}

            }

}

 

 

See the output:

8

thank you..

Tags: ,