自定义异常类:

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);
}
}

 

相关文章:

  • 2022-02-10
  • 2022-02-10
  • 2021-10-24
  • 2021-12-31
  • 2021-10-16
  • 2022-12-23
猜你喜欢
  • 2022-02-10
  • 2022-02-10
  • 2022-02-10
  • 2021-12-17
  • 2022-02-10
  • 2022-02-10
相关资源
相似解决方案