public void testFinally(){
System.out.println(setOne().toString());
}
protected StringBuilder setOne(){
StringBuilder builder=new StringBuilder();
try{
builder.append("Cool");
return builder.append("Return");
}finally{
builder=null; /* ;) */
}
}Run Code Online (Sandbox Code Playgroud)
为什么输出是CoolReturn,而不是null?
此致,
Mahendra Athneria
这些风险中的任何一个都有风险吗?一个更好吗?或者它是你打印出来并投掷飞镖决定的东西之一?
我想这样做,因为我明白了最终的工作方式:
try {
stuff that changes something...
}
catch (System.Exception ex) {
something.worked = false;
something.err = ex.Message;
}
finally {
stuff.close();
return something;
}
Run Code Online (Sandbox Code Playgroud)
但我见过:
try {
stuff that changes something...
return something;
}
catch (System.Exception ex) {
something.worked = false;
something.err = ex.Message;
return something;
}
finally {
stuff.close();
}
Run Code Online (Sandbox Code Playgroud) 我在try子句中有一个return语句:
def f():
try:
return whatever()
finally:
pass # How do I get what `whatever()` returned in here?
Run Code Online (Sandbox Code Playgroud)
是否有可能在finally子句中获得返回值?
这更像是一个理论问题,所以我不是在寻找一种解决方法,比如将它保存到变量中.
由于finally在C++中没有,你必须使用RAII设计模式,如果你希望你的代码是异常安全的.一种方法是使用像这样的本地类的析构函数:
void foo() {
struct Finally {
~Finally() { /* cleanup code */ }
} finalizer();
// ...code that might throw an exception...
}
Run Code Online (Sandbox Code Playgroud)
与直接解决方案相比,这是一个很大的优势,因为您不必编写清理代码2次:
try {
// ...code that might throw an exception...
// cleanup code (no exception)
} catch (...) {
// cleanup code (exception)
throw;
}
Run Code Online (Sandbox Code Playgroud)
本地类解决方案的一大缺点是您无法直接访问清理代码中的局部变量.因此,如果您需要访问它们,它会使您的代码膨胀很多,无论如何:
void foo() {
Task* task;
while (task = nextTask()) {
task->status = running;
struct Finally {
Task* task;
Finally(Task* task) : task(task) {}
~Finally() { task->status …Run Code Online (Sandbox Code Playgroud) 这是在标准C++中实现类似Last的行为的好方法吗?(没有特殊指针)
class Exception : public Exception
{ public: virtual bool isException() { return true; } };
class NoException : public Exception
{ public: bool isException() { return false; } };
Object *myObject = 0;
try
{
// OBJECT CREATION AND PROCESSING
try
{
myObject = new Object();
// Do something with myObject.
}
// EXCEPTION HANDLING
catch (Exception &e)
{
// When there is an excepion, handle or throw,
// else NoException will be thrown.
}
throw NoException();
}
// …Run Code Online (Sandbox Code Playgroud) 我有一种情况,我希望无论发生什么事情都要执行某些代码,但我需要将异常传递到堆栈中以便稍后处理.以下是:
try
{
// code
}
finally
{
// code that must run
}
Run Code Online (Sandbox Code Playgroud)
只是忽略任何异常,还是会将它们传递给它们?我的测试似乎表明他们仍然被传递,但我想确定我不是疯了.
编辑:我的问题不是关于何时以及最终是否会执行,而是关于异常是否仍然被抛出,但现在已经得到了回答.
int i=0;
try{
int j = 10/i;
}
catch(IOException e){}
finally{
Console.WriteLine("In finally");
Console.ReadLine();
}
Run Code Online (Sandbox Code Playgroud)
在VS2008中按F5时,似乎不执行finally块.我在控制台应用程序中使用此代码.
我知道如何使用try-catch-finally.但是我没有得到使用的进展,finally因为我总是可以在try-catch块之后放置代码.有没有明确的例子?
试试这段代码.为什么getValueB()返回1而不是2?毕竟,increment()函数被调用两次.
public class ReturningFromFinally
{
public static int getValueA() // This returns 2 as expected
{
try { return 1; }
finally { return 2; }
}
public static int getValueB() // I expect this to return 2, but it returns 1
{
try { return increment(); }
finally { increment(); }
}
static int counter = 0;
static int increment()
{
counter ++;
return counter;
}
public static void main(String[] args)
{
System.out.println(getValueA()); // prints 2 as expected
System.out.println(getValueB()); …Run Code Online (Sandbox Code Playgroud) JVM规范的某些部分建议操作JSR(Jump SubRoutine),JSR_W(Jump SubRoutine Wide)和RET(从子例程返回)仅可用于类文件版本50.0(JDK 1.6):
(本节假设编译器生成版本号为50.0或更低版本的类文件,以便可以使用jsr指令.另请参见§4.10.2.5.)
然后:
要实现
try-finallyconstruct,生成class版本号为50.0或更低版本的文件的Java编程语言的编译器可以使用异常处理工具和两个特殊指令:jsr("跳转到子例程")和ret("从子例程返回" ").
另一方面,操作码描述本身并未说明这些功能的弃用.引用的文本只说明过去50.0版本中的版本如何,但在此之后并没有明确说明事态.
这个评论(对于询问这种弃用或删除背后的动机的问题)表明了类似的混淆程度,所以显然我并不是唯一一个寻找此问题的人.