Java中的异常处理(Exception handling in Java)
我有关于try,catch和Java的问题。 请考虑以下情形:
try{
//Some code here that throws IOExceotion
}
catch (IOException ex){
System.out.println("Line 1: IOException Encountered!");
throw ex;
}
finally {
System.out.println("Line 2: I am always executed!");
}
上面的代码片段的输出是什么? 我会看到:
Line 1: IOException Encountered!
Line 2: I am always executed!
还是会的?
Line 2: I am always executed!
Line 1: IOException Encountered!
或者它只是(因为我们在catch块中抛出)
Line 1: IOException Encountered!
基本上,我没有找到一个示例,其中catch块中有一个“throw”,最后阻塞catch块(如上例所示)。 任何人都可以对此有所了解吗?
谢谢。
I have question about try, catch and finally in Java. Consider the following scenario:
try{
//Some code here that throws IOExceotion
}
catch (IOException ex){
System.out.println("Line 1: IOException Encountered!");
throw ex;
}
finally {
System.out.println("Line 2: I am always executed!");
}
What would be the output of the code snippet above? Am I going to see:
Line 1: IOException Encountered!
Line 2: I am always executed!
or would it be
Line 2: I am always executed!
Line 1: IOException Encountered!
Or would it be just (since we have a throw in the catch block)
Line 1: IOException Encountered!
Basically, I haven't found an example where there is a "throw" in the catch block and finally block following the catch block (like the example above). Can anyone shed some light on it?
Thanks.
原文:https://stackoverflow.com/questions/3613422
更新时间:2019-12-09 14:39
最满意答案
你会看到第一个。 最后,块始终执行并作为最后一个执行。
You will see the first one. Finally block is executed always and as a last.
2010-08-31
相关问答
如果有一个一致的方法来处理这些方法(例如,你总是用相同的方式包装它们并重新抛出一个RuntimeException ,那么自定义的包装库可能是合适的方法。当有2-3种不同的方式时,这仍然可以工作来处理它们(通过为单个包装方法/类提供2-3个包装器方法(甚至是类))。 或者,如果两个或多个异常类型具有完全相同的处理代码,那么您可以尝试查找Java 7 以获得多重捕获 。 If there is a consistent way to handle those method (i.e. you alw
...
编译器告诉您可能抛出异常,并且您必须满足这种可能性。 编译器正在对您的代码进行静态分析。 它无法分辨代码在实践中如何实际运行。 这可能令人沮丧。 例如,如果我写: new URL("http://www.stackoverflow.com");
编译器会坚持我捕获MalformedURLException 。 很明显URL很好,但编译器警告我,因为我可以使用以下方法构造一个URL对象: new URL(potentiallyDubiousUserInput);
而且我无法保证该字符串poten
...
我想知道是否必须添加throws并显示在B类中抛出异常? 如果您的意思是: public String getfoo(String foo2) throws SystemException { ... }
然后答案是否定的。 您不必声明该方法抛出SystemException ,因为它不会。 它在Class A抛出/处理,并且不会传播到Class B 。 但是,如果您没有处理Class A的异常,则必须在Class A的方法签名中指定您的异常(如果它是Checked Exception 。 然后
...
如果 socket = new Socket(hostName, 80);
抛出一个异常,然后socket将为空和 proxyConnection.bind(socket, params);
会抛出一个NullPointerException ,你不知道。 If socket = new Socket(hostName, 80);
throws an exception then socket will be null and proxyConnection.bind(socket, par
...
你会看到第一个。 最后,块始终执行并作为最后一个执行。 You will see the first one. Finally block is executed always and as a last.
例外的目的是让程序明白发生了一些不可思议的事情,因此通常在程序中接下来的事情很可能是错误的。 你所称的功能无法给你一个真正的答案,而你依赖于这个答案,所以它必须阻止你。 有两种方法可以结束:处理异常(通过catch块),或者整个程序停止。 如果你的程序在这些事情发生时不知道该怎么做,最好什么都不做。 让异常崩溃的线程,然后你可以稍后检查崩溃日志,并找出崩溃的程序和原因。 如果你的程序不能处理错误,Exception的“崩溃线程”行为可以让你看到什么错误是未处理的,所以你可以改变程序使它能够在将来处
...
查看您正在调用的File构造函数的文档。 声明抛出的唯一例外是NullPointerException 。 因此它不能抛出FileNotFoundException ,这就是你得到错误的原因。 您无法尝试捕获已检查的异常,编译器可以证明该异常永远不会在相应的try块中抛出。 创建File对象不会检查它是否存在。 如果您打开文件(例如,使用new FileInputStream(...)则可能抛出FileNotFoundException ...但不仅仅是创建File对象。 Look at the
...
静态上下文是在没有该类的实际实例的情况下在类上运行的上下文。 你的主要方法是静态的,这意味着它只能访问静态变量。 但是,您的AlfaException 不是静态的。 这意味着它将被绑定到一个Except类的实例 - 你没有这个实例 。 因此你有两个选择: 使AlfaException也是静态的: public static class AlfaException extends Exception{} 。 这将使它驻留在静态作用域中,以便可以从静态函数访问它。 将所有main(...)方法逻辑移到
...
编译器在throw站点输出一个athrow指令( JVM Specification ),在方法的代码中输出一个Exceptions属性( #4.7.5 ),显示所有各种catch子句的位置,它们涵盖的指令范围以及他们捕获的异常类型。 The compiler outputs an athrow instruction (JVM Specification) at the throw site, and an Exceptions attribute (#4.7.5) in the code of
...
Catch{ . . . } Catch{ . . . }块与try{ . . . }关联try{ . . . } try{ . . . } try{ . . . }块。 catch块可以捕获从try块中抛出的异常 。 第一个catch块之后的其他catch块与try块没有关联,因此当你抛出异常时,它们不会被捕获。 或者main()不会捕获异常。 每个catch块的这种做法都会做你正在做的事情。 try{
try
{
f1();
}
catch(IndexOutOfBou
...