首页 > 学院 > 开发设计 > 正文

异常的捕获和实现自己的异常类

2019-11-18 11:59:57
字体:
来源:转载
供稿:网友

  /**
  * <p>Title: 捕捉异常和实现自己的异常类</p>
  * <p>Description: 通过继续Exception类来实现自己的异常类。并使用try-catch来捕捉这个异常。</p>
  * <p>Copyright: Copyright (c) 2003</p>
  * <p>Filename: </p>
  * @version 1.0
  */
  class MyException extends Exception {
  public MyException() {}
  public MyException(String msg) {
  super(msg);
  }
  public MyException(String msg, int x) {
  super(msg);
  i = x;
  }
  public int val() { return i; }
  PRivate int i;
  }
  
  public class DemoException {
  /**
  *<br>方法说明:使用MyException类中默认的构造器
  *<br>输入参数:
  *<br>返回类型:
  */
  public static void a() throws MyException {
  System.out.println(
  "Throwing MyException from a()");
  throw new MyException();
  }
  /**
  *<br>方法说明:使用MyException类中带信息的构造器
  *<br>输入参数:
  *<br>返回类型:
  */
  public static void b() throws MyException {
  System.out.println(
  "Throwing MyException from b()");
  throw new MyException("Originated in b()");
  }
  /**
  *<br>方法说明:使用了MyException中有编码的构造器
  *<br>输入参数:
  *<br>返回类型:
  */
  public static void c() throws MyException {
  System.out.println(
  "Throwing MyException from c()");
  throw new MyException(
  "Originated in c()", 47);
  }
  public static void main(String[] args) {
  try {
  a();
  } catch(MyException e) {
  e.getMessage();
  }
  try {
  b();
  } catch(MyException e) {
  e.toString();
  }
  try {
  c();
  } catch(MyException e) {
  e.printStackTrace();
  System.out.println("error code: " + e.val());
  }
  }
  } //end

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表