Java 異常的捕獲及處理(異常處理流程)
阿新 • • 發佈:2019-02-11
import java.util.Scanner; public class Demo { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.println("1:除法開始:"); try { System.out.println("請輸入除數:"); String num1 = in.next(); Integer x = Integer.parseInt(num1); System.out.println("請輸入被除數:"); String num2 = in.next(); Integer y = Integer.parseInt(num2); System.out.println("計算結果:" + (x / y)); } catch (ArrayIndexOutOfBoundsException e) { e.printStackTrace(); } catch (NumberFormatException e) { e.printStackTrace(); } catch (ArithmeticException e) { e.printStackTrace(); } finally { System.out.println("不管是否異常都執行"); } System.out.println("計算結束!"); in.close(); } }
Java中異常處理流程: