hai

Discussion in 'Java' started by chanduvit, Jul 17, 2007.

  1. chanduvit

    chanduvit New Member

    Joined:
    Jul 16, 2007
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    hai iam learning java now

    i have doubt in java exception handling

    when we create our own exceptions what we are doing is declare our own exception class and make it as subclass of Exception class.Here is a sample program.This is ok.

    Code:
    class myexception2 extends Exception
    {
       myexception2(String s)
       {
          System.out.println("\n Untamed exception is thrown out:"+s);
       }
     public static void main(String args[])
     {
       myexception2 me=new myexception2("Lion");
       try
        {
          throw me;
        }
     catch(myexception2 e)
     {
        System.out.println("\n I caught that exception and tamed it:"+e);
     }
    }
    }

    Here is another version of creating our own exception class using Throwable class

    Code:
    class myexception1
    {
      public static void main(String args[])
      {
        Throwable myexpn=new Throwable("Help me");
        try
         {
           System.out.println("\n A new expception is thrown");
           throw myexpn;
        }
       catch(Throwable e)
       {
         System.out.println("\n the exception is caught here \n \n The exception is"+e);
       }
     }
    }

    In this program instead of making class myexception1 as subclass of throwable ,an object of throwable class is created and the program is executed correctly.How it could correct workly with out declaring class myexception1 as subclass to Throwable class.

    what is the difference between the above two programs?

    please help me.

    Thanks in advance.
     
    Last edited by a moderator: Jul 17, 2007

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice