Java_基础—try...catch的方式处理异常1
生活随笔
收集整理的這篇文章主要介紹了
Java_基础—try...catch的方式处理异常1
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
package com.soar.exception;public class Demo2_Exception {/** * A:異常處理的兩種方式* a:try…catch…finally的三種搭配* ① try catch* ② try catch finally* ③ try finally * b:throws* B:try...catch處理異常的基本格式* try…catch…finally* C:案例演示* try...catch的方式處理1個異常try:用來檢測異常catch:用來捕獲異常finally:釋放資源世界上最真情的相依,就是你在try,我在catch,無論你發(fā)神馬脾氣,我都靜靜接受,默默處理在try中出現(xiàn)了問題,再找catch,如果沒有問題,就不找catch當(dāng)通過try-catch將問題處理了,程序會繼續(xù)執(zhí)行。*/public static void main(String[] args) {Demo_div d = new Demo_div();try{int x = d.div(10, 0); System.out.println(x);}catch(ArithmeticException a){ //ArithmeticException a = new ArithmeticException(); System.out.println("出錯了,除數(shù)為0啦"); //出錯了,除數(shù)為0啦} System.out.println("這句話會被執(zhí)行嗎?"); //這句話會被執(zhí)行嗎?}
}
class Demo_div{//除法運算public int div(int a, int b){ //a=10, b=0return a/b; //10/0 被除數(shù)是10,除數(shù)是0,當(dāng)除數(shù)是0的時候,違背了算數(shù)運算法則,拋出異常//new ArithmeticException("/ by zero");}
}
轉(zhuǎn)載于:https://www.cnblogs.com/soarsir/p/7662581.html
總結(jié)
以上是生活随笔為你收集整理的Java_基础—try...catch的方式处理异常1的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: (转)linux dumpe2fs命令
- 下一篇: Oracle死锁情况