جاوا-چگونه کلاس Exception مورد نظر خود را پیاده سازی کنیم؟

در این sample نشان داده شده است که چگونه می توان کلاس Exception مورد نظر خود را نوشت و آن را Throw کرد: 

public class MyException extends Exception
{
  public String toString(String str1)
  {
   return str1;
  }
}
public class Test
{
 public static void meth1(int a, int b) throws MyException
  {
   if (a<b) throw new MyException();
  }
 public static void main(String arg[])
  {
   try
    {
      Test.meth1(1,2);   
    }catch(MyException e)
    {
      System.out.println(e.toString("It is my Exception Test"));
      e.printStackTrace();
    }
  }
}

نظرات 0 + ارسال نظر
برای نمایش آواتار خود در این وبلاگ در سایت Gravatar.com ثبت نام کنید. (راهنما)
ایمیل شما بعد از ثبت نمایش داده نخواهد شد