Java 异常处理;试一试
这是我的密码:Java 异常处理;试一试,java,oop,exception-handling,try-catch-finally,Java,Oop,Exception Handling,Try Catch Finally,这是我的密码: class FinallyDemo { static void myMethod(int n) throws Exception{ try { switch(n) { case 1: System.out.println("1st case"); return; case 3:
class FinallyDemo {
static void myMethod(int n) throws Exception{
try {
switch(n) {
case 1:
System.out.println("1st case");
return;
case 3:
System.out.println("3rd case");
throw new RuntimeException("3!");
case 4:
System.out.println("4th case");
throw new Exception("4!");
case 2:
System.out.println("2nd case");
}
catch (RuntimeException e) {
System.out.print("RuntimeException: ");
System.out.println(e.getMessage());
} finally {
System.out.println("try-block entered.");
}
}
public static void main(String args[]){
for (int i=1; i<=4; i++) {
try {
FinallyDemo.myMethod(i);
} catch (Exception e){
System.out.print("Exception caught: ");
System.out.println(e.getMessage());
}
System.out.println();
}
}
}
class FinallyDemo{
静态void myMethod(int n)引发异常{
试一试{
开关(n){
案例1:
系统输出打印号(“第一个案例”);
返回;
案例3:
System.out.println(“第三种情况”);
抛出新的RuntimeException(“3!”);
案例4:
System.out.println(“第四种情况”);
抛出新异常(“4!”);
案例2:
系统输出打印号(“第二种情况”);
}
捕获(运行时异常e){
System.out.print(“RuntimeException:”);
System.out.println(e.getMessage());
}最后{
System.out.println(“try block entered”);
}
}
公共静态void main(字符串参数[]){
对于(int i=1;i当且仅当捕获了抛出的异常类型时(或者如果它扩展了RuntimeException
),您不需要throws
子句。在您的情况下,您可以通过语句throw new exception(“4!”;
,抛出异常,但只捕获类型RuntimeException
如果为异常添加catch块
,则不再需要throws
子句。例如:
static void myMethod(int n) throws Exception{
try {
switch(n) {
case 1:
System.out.println("1st case");
return;
case 3:
System.out.println("3rd case");
throw new RuntimeException("3!");
case 4:
System.out.println("4th case");
throw new Exception("4!");
case 2:
System.out.println("2nd case");
}
} catch (RuntimeException e) {
System.out.print("RuntimeException: ");
System.out.println(e.getMessage());
} catch(Exception e) {
System.out.print("Exception: ");
System.out.println(e.getMessage());
} finally {
System.out.println("try-block entered.");
}
}
如果并且仅当捕获了抛出的异常类型(或者如果它扩展了RuntimeException
),则不需要使用throws
子句。在您的情况下,可以使用语句throw new exception(“4!”);
,抛出一个exception
,但只捕获类型RuntimeException
如果为异常添加catch块
,则不再需要throws
子句。例如:
static void myMethod(int n) throws Exception{
try {
switch(n) {
case 1:
System.out.println("1st case");
return;
case 3:
System.out.println("3rd case");
throw new RuntimeException("3!");
case 4:
System.out.println("4th case");
throw new Exception("4!");
case 2:
System.out.println("2nd case");
}
} catch (RuntimeException e) {
System.out.print("RuntimeException: ");
System.out.println(e.getMessage());
} catch(Exception e) {
System.out.print("Exception: ");
System.out.println(e.getMessage());
} finally {
System.out.println("try-block entered.");
}
}
是的,前提是您捕获了该方法可以抛出的所有异常类型
在代码中,您抛出了一个异常
,但没有为它提供一个捕获
块(您只捕获运行时异常
),因此您必须将方法声明为抛出异常
您需要:
...
catch (RuntimeException e) {
System.out.print("RuntimeException: ");
System.out.println(e.getMessage());
} catch (Exception e) {
System.out.print("Exception: ");
System.out.println(e.getMessage());
} finally {
...
是的,前提是您捕获了该方法可以抛出的所有异常类型
在代码中,您抛出了一个异常
,但没有为它提供一个捕获
块(您只捕获运行时异常
),因此您必须将方法声明为抛出异常
您需要:
...
catch (RuntimeException e) {
System.out.print("RuntimeException: ");
System.out.println(e.getMessage());
} catch (Exception e) {
System.out.print("Exception: ");
System.out.println(e.getMessage());
} finally {
...
在您的示例中,案例4抛出异常,而在catch中您只是捕获RuntimeException。由于没有catch for exception,因此您的方法需要声明它抛出异常。
若要为异常添加捕获,则不需要抛出异常。这将起作用
static void myMethod(int n) {
try {
switch (n) {
case 1:
System.out.println("1st case");
return;
case 3:
System.out.println("3rd case");
throw new RuntimeException("3!");
case 4:
System.out.println("4th case");
throw new Exception("4!");
case 2:
System.out.println("2nd case");
}
} catch (RuntimeException e) {
System.out.print("RuntimeException: ");
System.out.println(e.getMessage());
} catch (Exception e) {
System.out.print("Exception: ");
System.out.println(e.getMessage());
}
finally {
System.out.println("try-block entered.");
}
}
在您的示例中,案例4抛出异常,而在catch中您只是捕获RuntimeException。由于没有catch for exception,因此您的方法需要声明它抛出异常。
若要为异常添加捕获,则不需要抛出异常。这将起作用
static void myMethod(int n) {
try {
switch (n) {
case 1:
System.out.println("1st case");
return;
case 3:
System.out.println("3rd case");
throw new RuntimeException("3!");
case 4:
System.out.println("4th case");
throw new Exception("4!");
case 2:
System.out.println("2nd case");
}
} catch (RuntimeException e) {
System.out.print("RuntimeException: ");
System.out.println(e.getMessage());
} catch (Exception e) {
System.out.print("Exception: ");
System.out.println(e.getMessage());
}
finally {
System.out.println("try-block entered.");
}
}
在抛出异常的方法中不尝试catch块阻止在抛出异常的方法中写入“抛出异常”吗
不,您始终可以声明抛出异常,即使您不这样做
除此之外,允许子类抛出它们(因为不允许它们添加额外的抛出子句)也是很有用的。它还允许您以后在不更改异常接口的情况下更改实现
在抛出异常的方法中不尝试catch块阻止在抛出异常的方法中写入“抛出异常”吗
不,您始终可以声明抛出异常,即使您不这样做
除此之外,允许子类抛出它们(因为不允许它们添加额外的抛出子句)非常有用。它还允许您以后在不更改异常接口的情况下更改实现。现在有两种类型的异常
例外的子类
RuntimeException的子类
Exception的子类称为checked Exception,编译器确保这些子类在try/catch块中管理,或者通过修饰符在方法上抛出Exception(或子类)
RuntimeException的子类称为unchecked异常,编译不需要任何机制来管理它
现在,如果在方法上使用修饰符抛出异常(或子类),编译器将要求您使用try/catch来管理它。现在有两种类型的异常
例外的子类
RuntimeException的子类
Exception的子类称为checked Exception,编译器确保这些子类在try/catch块中管理,或者通过修饰符在方法上抛出Exception(或子类)
RuntimeException的子类称为unchecked异常,编译不需要任何机制来管理它
现在如果使用修饰符抛出异常(或子类)在方法上,编译器将要求您使用try/catch对其进行管理。由于您在开关中同时抛出RuntimeException
和Exception
,因此您需要捕获这两个异常,或者方法需要抛出异常
,以便可以在调用myMethod
的方法中处理它
要捕获这两种情况,请使用:
catch (RuntimeException e) {
System.out.print("RuntimeException: ");
System.out.println(e.getMessage());
}catch (Exception e) {
System.out.print("Exception: ");
System.out.println(e.getMessage());
}
确保捕获的异常始终是最后一个,否则它也将捕获运行时异常,因为它扩展了异常因为您在开关中同时抛出运行时异常和异常,您要么需要捕获这两个异常,要么方法需要抛出异常n
以便可以在方法调用中处理它<