2020.10.31
1.
package first; import javax.swing.*; class AboutException{ public static void main(String[] a) { int i=1, j=0, k; //k=i/j; try { k = i/j; // Causes division-by-zero exception //throw new Exception("Hello.Exception!"); } catch ( ArithmeticException e) { System.out.println("被0除. "+ e.getMessage()); } catch (Exception e) { if (e instanceof ArithmeticException) System.out.println("被0除"); else { System.out.println(e.getMessage()); } } finally { JOptionPane.showConfirmDialog(null,"OK"); } } }
总结:
(1)把可能会发生错误的代码放进try语句块中。
(2)当程序检测到出现了一个错误时会抛出一个异常对象。异常处理代码会捕获并处理这个错误。 catch语句块中的代码用于处理错误。
(3)当异常发生时,程序控制流程由try语句块跳转到catch语句块。
(4)不管是否有异常发生,finally语句块中的语句始终保证被执行。
(5)如果没有提供合适的异常处理代码,JVM将会结束掉整个应用程序。
2.
1 public class CatchWho { 2 public static void main(String[] args) { 3 try { 4 try { 5 throw new ArrayIndexOutOfBoundsException(); 6 } 7 catch(ArrayIndexOutOfBoundsException e) { 8 System.out.println( "ArrayIndexOutOfBoundsException" + "/内层try-catch"); 9 } 10 11 throw new ArithmeticException(); 12 } 13 catch(ArithmeticException e) { 14 System.out.println("发生ArithmeticException"); 15 } 16 catch(ArrayIndexOutOfBoundsException e) { 17 System.out.println( "ArrayIndexOutOfBoundsException" + "/外层try-catch"); 18 } 19 } 20 }
3.
1 public class CatchWho2 { 2 public static void main(String[] args) { 3 try { 4 try { 5 throw new ArrayIndexOutOfBoundsException(); 6 } 7 catch(ArithmeticException e) { //不同 8 System.out.println( "ArrayIndexOutOfBoundsException" + "/内层try-catch"); 9 } 10 throw new ArithmeticException(); //没有运行 11 } 12 catch(ArithmeticException e) { 13 System.out.println("发生ArithmeticException"); 14 } 15 catch(ArrayIndexOutOfBoundsException e) { 16 System.out.println( "ArrayIndexOutOfBoundsException" + "/外层try-catch"); 17 } 18 } 19 }
4.
1 public class EmbededFinally { 2 3 4 public static void main(String args[]) { 5 6 int result; 7 8 try { 9 10 System.out.println("in Level 1"); 11 12 13 try { 14 15 System.out.println("in Level 2"); 16 // result=100/0; //Level 2 17 18 try { 19 20 System.out.println("in Level 3"); 21 22 result=100/0; //Level 3 23 24 } 25 26 catch (Exception e) { 27 28 System.out.println("Level 3:" + e.getClass().toString()); 29 30 } 31 32 33 finally { 34 35 System.out.println("In Level 3 finally"); 36 37 } 38 39 40 // result=100/0; //Level 2 41 42 43 } 44 45 catch (Exception e) { 46 47 System.out.println("Level 2:" + e.getClass().toString()); 48 49 } 50 finally { 51 52 System.out.println("In Level 2 finally"); 53 54 } 55 56 // result = 100 / 0; //level 1 57 58 } 59 60 catch (Exception e) { 61 62 System.out.println("Level 1:" + e.getClass().toString()); 63 64 } 65 66 finally { 67 68 System.out.println("In Level 1 finally"); 69 70 } 71 72 } 73 74 }
5.
1 public class SystemExitAndFinally { 2 3 4 public static void main(String[] args){ 5 6 try{ 7 System.out.println("in main"); 8 throw new Exception("Exception is thrown in main"); 9 //System.exit(0); 10 }catch(Exception e) 11 12 { 13 14 System.out.println(e.getMessage()); 15 16 System.exit(0); 17 18 } 19 20 finally 21 22 { 23 24 System.out.println("in finally"); 25 26 } 27 28 } 29 30 31 }
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步