自定义异常类。
一、第一种定义方式,继承Exception类
public class MyFirstException extends Exception {
 public MyFirstException() {
  super();
 }
 public MyFirstException(String msg) {
  super(msg);
 }
 public MyFirstException(String msg, Throwable cause) {
  super(msg, cause);
 }
 public MyFirstException(Throwable cause) {
  super(cause);
 }
 //自定义异常类的主要作用是区分异常发生的位置,当用户遇到
    //异常时,根据异常名就可以知道哪里有异常,根据异常提示信
   //息进行修改。
}
二、 第二种定义方式:继承Throwable 类
 
public class MySecondException extends Throwable {
 public MySecondException() {
  super();
 }
 public MySecondException(String msg) {
  super(msg);
 }
 public MySecondException(String msg, Throwable cause) {
  super(msg, cause);
 }
 public MySecondException(Throwable cause) {
  super(cause);
 }
}

三、测试
/**
 * 自定义异常类的使用
 * @author new
 *
 */
public class TestMyException {
 public static void firstException() throws MyFirstException{
  throw new MyFirstException("\"firstException()\" method occurs an exception!");
 }
 
 public static void secondException() throws MySecondException{
  throw new MySecondException("\"secondException()\" method occurs an exception!");
 }
 public static void main(String[] args) {
  try {
   TestMyException.firstException();
   TestMyException.secondException();
  } catch (MyFirstException e1){
   System.out.println("Exception: " + e1.getMessage());
   e1.printStackTrace();
  } catch (MySecondException e2){
   System.out.println("Exception: " + e2.getMessage());
   e2.printStackTrace();
  }
 //当一个try块后面跟着多个catch块时,如果发生的异常匹配第一个catch块的参数,便将异常处理权利交给第一个catch块。
 //如果发生的异常与第一个catch块不匹配,便看是否与第二个catch块匹配,依次下去,如果到最后依然无法匹配该异常,
 //便需要在方法声明中添加一条throw语句,将该异常抛出。
 //因此,在有多个catch块,而且每次处理的异常类型具有继承关系时,应该首先catch子类异常,再catch父类异常。
 //比如,如果MySecondException继承MyFirstException,那么最好将catch (MySecondException e2)放在前面,
 //把catch (MyFirstException e1)放在后面。
}
}

异常处理方式:
  1. OutputStreamWriter out = ...       
  2. java.sql.Connection conn = ...       
  3. try {       
  4.  Statement stat = conn.createStatement();       
  5.  ResultSet rs = stat.executeQuery(       
  6. "select uid, name from user");       
  7.  while (rs.next())       
  8.  {       
  9. out.println("ID:" + rs.getString("uid") + ",姓名: " + rs.getString("name"));       
  10.  }       
  11. }       
  12. catch(SQLException sqlex)       
  13. {       
  14.  out.println("警告:数据不完整");       
  15.  throw new ApplicationException("读取数据时出现SQL错误", sqlex);       
  16. }       
  17. catch(IOException ioex)       
  18. {       
  19.  throw new ApplicationException("写入数据时出现IO错误", ioex);       
  20. }       
  21. finally       
  22. {       
  23.  if (conn != null) {       
  24. try {       
  25.  conn.close();       
  26. }       
  27. catch(SQLException sqlex2)       
  28. {       
  29.  System.err(this.getClass().getName() + ".mymethod - 不能关闭数据库连接: " + sqlex2.toString());       
  30. }       
  31.  }       
  32.      
  33.  if (out != null) {       
  34. try {       
  35.  out.close();       
  36. }       
  37. catch(IOException ioex2)       
  38. {       
  39.  System.err(this.getClass().getName() + ".mymethod - 不能关闭输出文件" + ioex2.toString());       
  40. }       
  41.  }       
  42. }  
posted on 2012-07-13 14:03  星^_^風  阅读(140)  评论(0编辑  收藏  举报