From 01858c81a955057b9a3cdd163e4c1d922e0914eb Mon Sep 17 00:00:00 2001 From: xuxin <840198532@qq.com> Date: Wed, 4 Feb 2026 10:59:50 +0800 Subject: [PATCH] =?UTF-8?q?=E8=BF=9B=E9=98=B6day04-throws=E5=85=B3?= =?UTF-8?q?=E9=94=AE=E5=AD=97=E7=9A=84=E4=BD=9C=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/com/inmind/throws03/ThrowsDemo05.java | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 javaSE-day05/src/com/inmind/throws03/ThrowsDemo05.java diff --git a/javaSE-day05/src/com/inmind/throws03/ThrowsDemo05.java b/javaSE-day05/src/com/inmind/throws03/ThrowsDemo05.java new file mode 100644 index 0000000..a6adffa --- /dev/null +++ b/javaSE-day05/src/com/inmind/throws03/ThrowsDemo05.java @@ -0,0 +1,38 @@ +package com.inmind.throws03; +/* +throws关键字 + throws:声明一个异常,提醒方法的调用者,该方法可能会抛出某些异常,但是也有可能不抛出异常 + throw:真的抛出了一个真正的异常 + + throws关键字的语法格式: + 方法修饰符 返回值类型 方法名(参数列表) throws 异常名,异常名...{ + 方法体 + } + + 注意点: + 1.方法中如果抛出一个编译时异常,一定要处理(try-catch)或者声明出去 + 2.如果调用了一个声明了编译时异常的方法,那么也一定要处理(try-catch)或者声明出去 + 3.如果方法中没有异常出现,我们也是可以声明 + + + */ +public class ThrowsDemo05 { + public static void main(String[] args) throws Exception { + method1(); + method3(); + System.out.println("程序结束"); + } + + public static void method1() throws Exception{ + //抛出一个编译时异常 + throw new Exception(); + } + + public static void method2() throws RuntimeException{ + //抛出一个运行时异常 + throw new RuntimeException(); + } + + public static void method3() throws Exception{ + } +}