Exception handling in java

Exception handling:

  • Exception in java is an indication of some unusual event.
  • Usually it indicates the error.
  • Various keywords used in handling the exception are :
  • try-A block of source code that is to be monitored for the exception.
  • catch  -The catch block handles the specific type of exception along with the     try block.
  • finally - It specifies the code that must be executed even though exception may or                may not occur.
  • throw - Used to throw specific exception from the program code.
  • throws - It specifies the exceptions that can be thrown by a particular method.

Syntax:

  1. class class_name

    {

    try

    {

    //Exception occures here

    }

    catch(Exception_type exception_variable)

    {

    //Exception handling here

    }

    }

Types of exception:

1) Built-in exceptions:

Before using exception:

  1. public class Divide_By_Zero {

        public static void main(String args[]) {

            int a = 0, b = 0, c = 0;

            a = 5;

            b = 0;

            c = a / b;

            System.out.println(a + " divide by zero of value is : " + c);

        }

    }

Output:

Handling above error by using exception:

  1. public class Divide_By_Zero {

        public static void main(String args[])

        {

            int a = 0,b=0,c=0;

            try

            {

                a=5;

                b=0;

                c=a/b; 

            }

            catch(ArithmeticException e)

            {

                System.out.println(a+" divide by zero is infinity.");

            }

        }

Output:

Another Example program:

  1. public class Divide_By_Zero {

        public static void main(String args[]) {

            int a[] = new int[3];

            try {

                for (int i = 1; i <= 3; i++) {

                    a[i] = i * i;

                    System.out.println("Arrsy index"+"\t"+"Added value");

                    System.out.println(i+"\t\t"+a[i]);

                }

            } catch (ArrayIndexOutOfBoundsException e) {

                System.out.println("Array index is out of bounds.");

            }

        }

    }

Output:

2) User-defined exception:

  • If you need to create user-defined exception, must be constructor method.

Example program:

  1. import java.lang.Exception;

    public class user_defined_exception extends Exception {

     

        public user_defined_exception(String msg) {

            super(msg);

        }

    }

    class Objects {

        public static void main(String args[]) {

            int age=15;

            try {

                if (age < 21) {

                    throw new user_defined_exception("Your age is very less than the condition.");

                }

            } catch (user_defined_exception e) {

                System.out.println("This is exception block.");

                System.out.println(e.getMessage());

     

            } finally {

                System.out.println("Finally exception block is end.");

            }

        }

    }

Output:
Share:

No comments:

Post a Comment

Recent Posts

Service Support

Need our help to Learn or Post New Concepts Contact me