Java异常类和自定义异常类

自定义异常类:

public class ExtendsException extends Exception {
private static final long serialVersionUID = 1L;
public ExtendsException(String msg)
  {
      super(msg);
  }
}

入口类:

public class ExceptionTest {
public static void main(String args[]) 
{
  /*
   * 段落级的异常*/
  int a=4,b=0;
  try
  {
      if(b==0)
          throw new ExtendsException("自定义异常");
      else
          System.out.println(a+"/"+b+"="+a/b);
  }
  catch(Exception e)
  {
      System.out.println("异常信息:"+e);
  }
  
  try
  {
  /*
   * 方法级的异常*/
  Exec(a,b);
  }
  catch(Exception e)
  {
      System.out.println("异常信息:"+e);
  }
}
public static void Exec(int m,int n) throws Exception
{
  int x;
  x=m/n;
  System.out.println(m+"/"+n+"="+x);
}
}

 

posted @ 2013-08-31 11:19  fang_beny  阅读(286)  评论(0编辑  收藏  举报