Java關鍵字
用於try..catch..finally塊,進行異常捕獲。
基本介紹
- 中文名:嘗試
- 外文名:try
- 套用:用於捕獲異常
基本信息
try { 語句塊1} catch (<? extends Throwable> e) { 語句塊2.1} catch (<? extends Throwable> e) { 語句塊2.2} catch (<? extends Throwable> e) { 語句塊2.3...} finally { 語句塊3}
public class Test { public static void main(String[] args) { int i = 10; try { System.out.println(i / 0); } catch(ArithmeticException ame) { ame.printStackTrace(); } finally { System.out.println("byebye"); } }}
作用關係
private int a = 0;private static int c1() { try { return 1; } finally { a++; }}
private static int c2() { try { return 1; // unreachable statement } finally { return 2; }}
private int a = 0;private static int c3() { try { return a + 10; } finally { a++; }}
int tmp = a + 10;return tmp;
private int a = 0;private static int change() { return ++a; private static int c4() { try { return change(); } finally { return change(); }}
try { int tmp = change(); return tmp; // unreachable statement} finally { int t = change(); return t;}