例如:
try
{
SomeObject someObject = new SomeObject();
someObject.dangerousMethod();
}
catch(Exception e)
{
}
someObject.anotherMethod(); //can't access someObject!
Run Code Online (Sandbox Code Playgroud)
但你可以在try/catch块之前声明它然后它工作正常:
SomeObject someObject;
try
{
someObject = new SomeObject();
someObject.dangerousMethod();
}
catch(Exception e)
{
}
someObject.anotherMethod(); //works fine
Run Code Online (Sandbox Code Playgroud)
我只是想知道这个的设计原因.为什么在try/catch块内创建的对象不在方法的其余部分范围内?也许我不会深入了解try/catch除了观看Exceptions投掷之外的工作方式.
例如:
if (true) try
{
// works as expected with both true and false, but is it legal?
}
catch (...)
{
// ...
}
Run Code Online (Sandbox Code Playgroud)
换句话说,将if -block放在if条件之后是否合法?
是否有一种不那么丑陋的方式来处理close()关闭两个流的异常:
InputStream in = new FileInputStream(inputFileName);
OutputStream out = new FileOutputStream(outputFileName);
try {
copy(in, out);
} finally {
try {
in.close();
} catch (Exception e) {
try {
// event if in.close fails, need to close the out
out.close();
} catch (Exception e2) {}
throw e; // and throw the 'in' exception
}
}
out.close();
}
Run Code Online (Sandbox Code Playgroud)
更新:所有上述代码都在一个try-catch内,感谢警告.
最后(在答案之后):
一个好的实用方法可以使用Execute Around成语(感谢Tom Hawtin).
而在if等中的其他语句中,如果块中只有一条指令,则可以避免使用大括号,而不能使用try ... catch块执行此操作:编译器不会购买它.例如:
try
do_something_risky();
catch (...)
std::cerr << "Blast!" << std::endl;
Run Code Online (Sandbox Code Playgroud)
使用上面的代码,g ++只是说它在do_something_risky()之前需要一个'{'.为什么这种行为的差异尝试...捕获,比如说,如果......其他?
谢谢!
我最近遇到了这个语法try-catchfor function.
struct A
{
int a;
A (int i) : a(i) // normal syntax
{
try {}
catch(...) {}
}
A () // something different
try : a(0) {}
catch(...) {}
void foo () // normal function
try {}
catch(...) {}
};
Run Code Online (Sandbox Code Playgroud)
两种语法都有效.除了编码风格之外,这些语法之间是否有任何技术差异?在任何方面,语法之一是否优于其他语法?
我运行了一个示例程序,确实调用了堆栈分配对象的析构函数,但这是否由标准保证?
为什么Java中的一些例外没有被捕获catch (Exception ex)?这是代码完全失败,出现未处理的异常.(Java版本1.4).
public static void main(String[] args) {
try {
//Code ...
} catch (Exception ex) {
System.err.println("Caught Exception");
ex.printStackTrace();
exitCode = app.FAILURE_EXIT_CODE;
}
finally {
app.shutdown();
}
System.exit(exitCode);
}
Run Code Online (Sandbox Code Playgroud)
我得到了 Exception in thread "main" java.lang.NoSuchMethodError
但这很有效
public static void main(String[] args) {
int exitCode = app.SUCCESS_EXIT_CODE;
try {
//Code ...
} catch (java.lang.NoSuchMethodError mex){
System.err.println("Caught NoSuchMethodError");
mex.printStackTrace();
exitCode = app.FAILURE_EXIT_CODE;
} catch (Exception ex) {
System.err.println("Caught Exception");
ex.printStackTrace();
exitCode = app.FAILURE_EXIT_CODE;
}
finally {
app.shutdown();
} …Run Code Online (Sandbox Code Playgroud) 我想知道一下C++ try/catch/finally块.我已经看到这些命令有两个下划线,如__try.但MVSC 2010项目也没有下划线.所以你什么时候需要这些下划线?
我是编码的主要菜鸟.我开始学习Python,但不是粉丝,而是转向我喜欢的Ruby.现在我正在尝试将一些python代码转换为ruby而我陷入了python的"尝试"是一个相当于python的"尝试"的红宝石
在Java中使用Readers和Streams总是让我感到困惑的一件事是该close()方法可以抛出异常.因为将close方法放在finally块中是个好主意,这需要一些尴尬的情况.我通常使用这种结构:
FileReader fr = new FileReader("SomeFile.txt");
try {
try {
fr.read();
} finally {
fr.close();
}
} catch(Exception e) {
// Do exception handling
}
Run Code Online (Sandbox Code Playgroud)
但我也看到了这种结构:
FileReader fr = new FileReader("SomeFile.txt");
try {
fr.read()
} catch (Exception e) {
// Do exception handling
} finally {
try {
fr.close();
} catch (Exception e) {
// Do exception handling
}
}
Run Code Online (Sandbox Code Playgroud)
我更喜欢第一个结构,因为只有一个挡块,它看起来更优雅.是否有理由更喜欢第二种或替代结构?
更新:如果我指出两者read并且close只抛出IOExceptions ,它会有所作为吗?因此,在我看来,如果读取失败,关闭将因同样的原因而失败.