java学习(139):多个catch块
生活随笔
收集整理的這篇文章主要介紹了
java学习(139):多个catch块
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
import java.sql.Connection;import java.io.IOException;
import java.sql.SQLException;//java異常處理
//異常
public class test82 {//定義方法聲明定義異常,在滿足條件時拋出異常對象,程序轉(zhuǎn)向異常處理public double count(double n, double m, Connection conn)throws ArithmeticException,SQLException {if (m == 0) {//如果除數(shù)等于0.則拋出異常實(shí)例throw new ArithmeticException("對不起。除數(shù)不能等于0");}if(conn==null||conn.isClosed()){throw new SQLException( "數(shù)據(jù)庫連接失敗" );}return n/m;}
}
測試類
import java.sql.Connection; import java.sql.SQLException;public class test80 {//定義了編譯異常的方法調(diào)用,必須進(jìn)行顯示處理public static void main(String[] args){test82 com=new test82();Connection conn = null;try {double t = com.count( 78, 2, conn );//子類異常必須放在前面}catch (SQLException e){String msg = e.getMessage();System.err.println( msg );e.printStackTrace();//打印異常軌跡}catch (Exception e){String msg = e.getMessage();System.err.println( msg );e.printStackTrace();//打印異常軌跡}finally {//強(qiáng)制執(zhí)行的代碼,通常執(zhí)行釋放被占用的資源System.out.println( "開始資源釋放" );try {conn.close();}catch (SQLException e){e.printStackTrace();}System.out.println( "資源釋放完畢" );}} }運(yùn)行結(jié)果
總結(jié)
以上是生活随笔為你收集整理的java学习(139):多个catch块的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: toolbar java_Java To
- 下一篇: html:(24):内联式css和嵌入式