From 2b4260771445414f4e7c6bbbda98f4fba9014f65 Mon Sep 17 00:00:00 2001 From: xuxin <840198532@qq.com> Date: Fri, 16 Oct 2026 14:16:35 +0800 Subject: [PATCH] =?UTF-8?q?s-day05--try..catch=E5=A4=84=E7=90=86=E5=A4=9A?= =?UTF-8?q?=E4=B8=AA=E5=BC=82=E5=B8=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/com/inmind/exception01/Demo09.java | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 s-day05/src/com/inmind/exception01/Demo09.java diff --git a/s-day05/src/com/inmind/exception01/Demo09.java b/s-day05/src/com/inmind/exception01/Demo09.java new file mode 100644 index 0000000..f677fa6 --- /dev/null +++ b/s-day05/src/com/inmind/exception01/Demo09.java @@ -0,0 +1,46 @@ +package com.inmind.exception01; +/* +try..catch处理多个异常 + + 在实际开发中,一段业务代码,可能会出现多种不同的异常,所以我们要进行多异常处理 + + try...catch的语法: + try{ + 可能会出现异常的代码或者方法 + }catch(指定异常类名 变量名){ + 捕获异常后,异常处理代码 + }catch(指定异常类名 变量名){ + 捕获异常后,异常处理代码 + }catch(指定异常类名 变量名){ + 捕获异常后,异常处理代码 + }..... + + 注意:在实际开发中,具体的异常处理,都是要进行相关的业务操作,而不是固定操作 + */ +public class Demo09 { + public static void main(String[] args) { + try { + System.out.println("try开始了------【1】"); + int i = 2; + if (i < 6) { +// throw new NullPointerException(); + throw new ClassCastException(); + } + if (i >= 6) { + throw new ArrayIndexOutOfBoundsException(); + } + System.out.println("try结束了------【2】"); + } catch (NullPointerException e) { + System.out.println("处理了空指针异常"); + } catch (ArrayIndexOutOfBoundsException e) { + System.out.println("处理了越界异常"); + } catch (Exception e){//托底操作 + System.out.println("除了空和越界以外的异常,都这么处理"); + } + finally { + System.out.println("finally"); + } + + System.out.println("程序结束"); + } +}