拿这个代码:
using System;
namespace OddThrow
{
class Program
{
static void Main(string[] args)
{
try
{
throw new Exception("Exception!");
}
finally
{
System.Threading.Thread.Sleep(2500);
Console.Error.WriteLine("I'm dying!");
System.Threading.Thread.Sleep(2500);
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
这给了我这个输出:
Unhandled Exception: System.Exception: Exception!
at OddThrow.Program.Main(String[] args) in C:\Documents and Settings\username
\My Documents\Visual Studio 2008\Projects\OddThrow\OddThrow\Program.cs:line 14
I'm dying!
Run Code Online (Sandbox Code Playgroud)
我的问题是:为什么未处理的异常文本出现在finally之前?在我看来,在我们甚至不知道这个异常未处理之前,最终应该在堆栈展开时被执行.注意对Sleep()的调用 - 这些是在打印出未处理的异常之后发生的,就像它执行以下操作一样:
根据C#标准,§8.9.5,这种行为是错误的:
我哪里错了?(我有一些自定义控制台错误消息,这是在路上.轻微,只是烦人,让我质疑语言......)
今天在大学时我们谈了一些try,catch并且finally.我对这两个例子感到困惑:
PrintWriter out = null;
try {
out = new PrintWriter(...); // We open file here
} catch (Exception e) {
e.printStackTrace();
} finally { // And we close it here
out.close();
}
Run Code Online (Sandbox Code Playgroud)
关闭文件finally和我们只是这样做有什么区别:
PrintWriter out = null;
try {
out = new PrintWriter(...); // We open file here
} catch (Exception e) {
e.printStackTrace();
}
out.close();
Run Code Online (Sandbox Code Playgroud)
catch之后的这段代码将始终执行.
你能给我一些很好的例子来说明我们何时使用finally以及何时将代码放入catch之后的差异?我知道最终将永远执行,但程序也会在catch块之后继续运行.
为什么foowhiz 中的异常被忽视,但bar引发了异常?
def foo():
try:
raise Exception('foo')
finally:
return
def bar():
try:
raise Exception('bar')
finally:
pass
foo()
bar()
Run Code Online (Sandbox Code Playgroud) 考虑这两个例子
<?php
function throw_exception() {
// Arbitrary code here
throw new Exception('Hello, Joe!');
}
function some_code() {
// Arbitrary code here
}
try {
throw_exception();
} catch (Exception $e) {
echo $e->getMessage();
}
some_code();
// More arbitrary code
?>
Run Code Online (Sandbox Code Playgroud)
和
<?php
function throw_exception() {
// Arbitrary code here
throw new Exception('Hello, Joe!');
}
function some_code() {
// Arbitrary code here
}
try {
throw_exception();
} catch (Exception $e) {
echo $e->getMessage();
} finally {
some_code();
}
// More arbitrary code …Run Code Online (Sandbox Code Playgroud) 我知道如何尝试,捕获和最终工作(大多数情况下),但我有一件事我想知道:在try-catch-finally之后返回语句会发生什么,而我们已经在try(或者catch)中返回了)?
例如:
public boolean someMethod(){
boolean finished = false;
try{
// do something
return true;
}
catch(someException e){
// do something
}
finally{
// do something
}
return finished;
}
Run Code Online (Sandbox Code Playgroud)
假设在尝试中没有出错,所以我们返回true.然后我们将进入最后我们做的事情,比如关闭连接,然后呢?
在我们在finally中做了一些东西之后,方法会停止吗(所以方法在try中返回true),或者方法会在finally之后继续,导致返回完成(这是假的)?
在此先感谢您的回复.
我检查到该守护进程的JVM治疗线程经常重复的传闻finally在一些特殊的方法块(他们不这样做,好吗?),当我读了这一点,从甲骨文的Java教程:
注意:如果JVM在执行
try或catch代码时退出,则该finally块可能无法执行.同样,如果执行try或catch代码的线程被中断或终止,即使应用程序作为一个整体继续,该finally块也可能不会执行.
(重点是我的.)关于打断的一点引起了我的注意!
我的信念是,如果一个线程在try/catch代码中并且被中断,那么我们要么(或最终进入)一个状态(例如睡眠,等待)我们最终投掷一个InterruptedException,或者我们不是,我们正常或异常退出,但在所有情况下,我们都会遵守该finally条款.
我错过了什么?finally在应用程序继续运行时,是否真的有一种方法可以让线程被中断然后跳过a ?
我正在cmd.exeWindows中运行我的Java应用程序.如果我通过按Ctrl-C强行停止进程,并且当时的代码在try块中运行,那么该finally块是否仍会被执行?
在我的测试中似乎是,它被执行了.
public class Test2 {
public static void main(String[] args) {
Test2 obj=new Test2();
String a=obj.go();
System.out.print(a);
}
public String go() {
String q="hii";
try {
return q;
}
finally {
q="hello";
System.out.println("finally value of q is "+q);
}
}
Run Code Online (Sandbox Code Playgroud)
为什么hii从函数返回后打印go(),在finally块中值已更改为"hello"?
该计划的输出是
finally value of q is hello
hii
Run Code Online (Sandbox Code Playgroud) 多年来一直在做Java,所以一直没有跟踪C++.已经最后条款中增加了C++异常处理的语言定义是什么?
是否有一种模仿Java的尝试/终极的青睐成语?
我还担心C++没有可能抛出的所有可能异常的最终超类型 - 比如Java的Throwable类.
我可以写:
try {
// do something
} catch(...) {
// alas, can't examine the exception
// can only do cleanup code and perhaps rethrow, ala:
throw;
}
Run Code Online (Sandbox Code Playgroud)
附录编辑:
我最终接受了得票最多的答案,即使用析构函数进行清理.当然,从我自己的评论来看,很明显我并不完全同意这一点.但是,C++就是这样,所以在我想到的应用程序中,我会或多或少地努力坚持共同的社区实践.我将使用模板类来包装尚未具有类析构函数的资源(即C库资源),从而赋予它们析构函数语义.
新的附加编辑:
嗯,而不是最后一个封闭功能或许?结合ScopeGuard方法的闭包(参见下面的答案之一)将是一种通过任意操作完成清理并访问清理代码的外部范围上下文的方法.清理可以用在Ruby编程中看到的成语方式来完成,它们在打开资源时提供清理块.C++不考虑关闭功能吗?
我正在攻读面向对象编程的测试,我想知道是否有任何案例,考虑到以下代码:
try {
do something
} catch (someException e) {
} finally {
do something
}
Run Code Online (Sandbox Code Playgroud)
该finally块不会执行?