import java.sql.Connection;import java.io.IOException;
import java.sql.SQLException;//java异常处理
//异常
public class test82 {//定义方法声明定义异常,在满足条件时抛出异常对象,程序转向异常处理public double count(double n, double m, Connection conn)throws ArithmeticException,SQLException {if (m == 0) {//如果除数等于0.则抛出异常实例throw new ArithmeticException("对不起。除数不能等于0");}if(conn==null||conn.isClosed()){throw new SQLException( "数据库连接失败" );}return n/m;}
}
测试类
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Collection;public class test83 {public static void main(String[] args){test82 com1=new test82();Collection conn=null;try {double t = com1.count( 78, 2, (Connection) conn );}catch (ArithmeticException| SQLException e){System.out.println( e.getClass().getName());e.printStackTrace();}}
}
运行结果