Publish your story - poem - jokes free online--click here

JAVA

Custom Exception in Java

Custom Exception

Java provides something called custom exception as name suggest we can create our own Exception type to throw the exception.
All we have to do is create a sub class of Exception class or Throwable.

Example :---

class custom extends Exception
{
            custom()
            {
                        System.out.println("there is no exception in this block");
            }
}
class test
{
            void divide()throws custom
            {
                        int a=34;
                        int b=0;
                        int c=a/b;
                        throw new custom();
            }
            void array()throws custom
            {
                        int a=34;
                        int b=0;
                        int c=a/b;
                        throw new custom();
            }
}
public class abc
{
            public static void main(String as[])
            {
                        test t = new test();
                        try
                        {
                                    t.divide();
                                    t.array();
                        }
                        catch(Exception e)
                        {
                                    System.out.println("Exception occurred "+e);
                        }
                        finally
                        {
                                    System.out.println("this is finally block it's always run");
                        }
            }
}


No comments:

Post a Comment