有没有像linux try catch一样的linux bash命令?或者linux shell总是继续?
try {
`executeCommandWhichCanFail`
mv output
} catch {
mv log
} finally {
rm tmp
}
Run Code Online (Sandbox Code Playgroud) 通过阅读本论坛中已经提到的与上述主题相关的所有问题(参见标题),我完全理解finally总是被称为.(除了System.exit和无限循环).但是,我想知道是否return在catch块中调用了a,然后return从finally块调用了另一个.
例如:
public static void main(String[]args) {
int a = new TestClass().absorbeTheValue();
}
int absorbeTheValue() {
try {
int a = 10/0;
if (a > 0) return 4;
} catch(Exception e) {
return 45;
} finally {
return 34;
}
}
Run Code Online (Sandbox Code Playgroud)
所以这里输出(当调用方法时)在任何情况下都是34.这意味着终于总能运行.我认为虽然其他"回归"根本没有运行.在很多帖子中,我发现最后将内容写入catch子句返回已写入的内容.我的理解是,一旦catch子句中的返回值即将被评估,控制流就会传递给finally子句,而子句又有另一个返回,这次返回将被评估,而不会将控制权传递给catch子句.通过这种方式,return在运行时唯一被调用的是最终返回.你同意吗?
甲return在finally不传递回控制到程序但返回值,并结束方法.我们可以这样说吗?
我怀疑我正在finally错误地使用该块,而且我不了解其目的的基本原理......
function myFunc() {
try {
if (true) {
throw "An error";
}
} catch (e) {
alert (e);
return false;
} finally {
return true;
}
}
Run Code Online (Sandbox Code Playgroud)
此函数将运行catch块,警告"一个错误",但然后返回true.为什么不返回虚假?
鉴于此代码:
String test() {
try {
return "1";
} finally {
return "2";
}
}
Run Code Online (Sandbox Code Playgroud)
语言规范是否定义了调用的返回值test()?换句话说:每个JVM中它总是一样的吗?
在Sun JVM中,返回值是2,但我想确定,这不依赖于VM.
我想知道一下C++ try/catch/finally块.我已经看到这些命令有两个下划线,如__try.但MVSC 2010项目也没有下划线.所以你什么时候需要这些下划线?
以下代码引发语法错误:
>>> for i in range(10):
... print i
... try:
... pass
... finally:
... continue
... print i
...
File "<stdin>", line 6
SyntaxError: 'continue' not supported inside 'finally' clause
Run Code Online (Sandbox Code Playgroud)
为什么continue在finally条款中不允许声明?
PS另一方面,这个其他代码没有问题:
>>> for i in range(10):
... print i
... try:
... pass
... finally:
... break
...
0
Run Code Online (Sandbox Code Playgroud)
如果重要,我使用的是Python 2.6.6.
当try-catch结束时,总是执行"finally"块,无论是否异常.但是,try-catch之外和之后的每一行代码总是被执行.那么,我为什么要使用finally语句呢?
例:
try {
//code...
} catch (Exception e) {
//code...
} finally {
System.out.println("This line is always printed");
}
System.out.println("Also this line is always printed !! So why to use 'finally'?? ");
Run Code Online (Sandbox Code Playgroud) 我们可以在finally块中使用return语句.这会导致任何问题吗?
是否可以通过finally抛出异常来确定代码当前是否在处理程序的上下文中执行?我非常喜欢使用IDisposable模式来实现入口/出口作用域功能,但是这种模式的一个问题是,如果在主体中发生异常,您可能不一定希望发生作用域结束行为using.我会找这样的东西:
public static class MyClass
{
public static void MyMethod()
{
using (var scope = MyScopedBehavior.Begin())
{
//Do stuff with scope here
}
}
}
public sealed class MyScopedBehavior : IDisposable
{
private MyScopedBehavior()
{
//Start of scope behavior
}
public void Dispose()
{
//I only want to execute the following if we're not unwinding
//through finally due to an exception:
//...End of scope behavior
}
public static MyScopedBehavior Begin()
{
return new MyScopedBehavior(); …Run Code Online (Sandbox Code Playgroud) 在Java 7的try-with-resources中,我不知道finally块和自动关闭发生了哪个顺序.订单是什么?
BaseResource b = new BaseResource(); // not auto-closeable; must be stop'ed
try(AdvancedResource a = new AdvancedResource(b)) {
}
finally {
b.stop(); // will this happen before or after a.close()?
}
Run Code Online (Sandbox Code Playgroud) finally ×10
java ×5
try-catch ×5
return ×2
c# ×1
c++ ×1
continue ×1
exception ×1
idisposable ×1
java-7 ×1
javascript ×1
python ×1
shell ×1
syntax ×1
syntax-error ×1