Go4Expert

Go4Expert (http://www.go4expert.com/)
-   Java (http://www.go4expert.com/forums/java/)
-   -   hai (http://www.go4expert.com/forums/hai-t5299/)

chanduvit 17Jul2007 12:40

hai
 
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.


All times are GMT +5.5. The time now is 14:38.