标签: try-catch-finally

C#:为什么要打扰'finally'条款?

可能重复:
为什么最后在C#中使用?

在C#中,有一个finally条款有什么意义?

例如.

try {
        // do something
    }
catch (Exception exc)
    {
        // do something
    }
// do something
Run Code Online (Sandbox Code Playgroud)

最后代码不会执行吗?一个finally街区有什么意义?

c# try-catch-finally

3
推荐指数
1
解决办法
900
查看次数

尝试抓点终于阻止?

最后使用有什么区别

void ReadFile(int index)
{
    // To run this code, substitute a valid path from your local machine
    string path = @"c:\users\public\test.txt";
    System.IO.StreamReader file = new System.IO.StreamReader(path);
    char[] buffer = new char[10];
    try
    {
        file.ReadBlock(buffer, index, buffer.Length);
    }
    catch (System.IO.IOException e)
    {
        Console.WriteLine("Error reading from {0}. 
           Message = {1}", path, e.Message);
    }
    finally
    {
        if (file != null)
        {
            file.Close();
        }
    }
    // Do something with buffer...
}
Run Code Online (Sandbox Code Playgroud)

而不是使用它?

void ReadFile(int index)
{
    // To run this code, substitute a …
Run Code Online (Sandbox Code Playgroud)

c# try-catch try-catch-finally

3
推荐指数
1
解决办法
558
查看次数

try-catch-finally vs抽象方法

在我们的系统中,我们有一个抽象类,我们称之为BasicAction,它包含几个抽象方法.其中最重要的是执行.它处理来自JSP页面的请求.主处理程序的工作方式如下:

// Sample 1: 
String actionName;// The name of action to use
Map<String, BasicAction> mapping;//The mapping of names and actual handlers

BasicAction action = mapping.get(actionName);

try {
  action.execute(request);//Handle the http request from the JSP page
} catch (Exception ex) {
  // Handle here any exceptions
}
Run Code Online (Sandbox Code Playgroud)

现在,一切看起来都很好,但基本上所有派生的处理程序都实现了相同的代码:

// Sample 1: 
public class ConcreteAction extends BasicAction {
  @Override
  public void execute(HttpServletRequest request) {
    // The managers provide the middle layer between 
    // web presentation and database
    TrafficManager …
Run Code Online (Sandbox Code Playgroud)

java try-catch-finally

3
推荐指数
1
解决办法
1025
查看次数

声明异常而不是处理它有什么意义

据我所知,如果您只声明一个已检查的异常,它将通过您的所有方法传播到main方法,并仍然中断您的正常程序流程,您的程序仍将停止工作.那么,为什么不总是使用try/catch处理已检查的异常...这样你的程序不会因异常而停止?为什么要在方法的签名中声明异常?对不起,我的英语不好

java exception try-catch throw try-catch-finally

3
推荐指数
1
解决办法
1379
查看次数

Try-Catch错误目标C.

我试图从给定的Instagram图片中获取字幕,但是如果没有标题,应用程序会抛出异常并崩溃.我将如何实现@try@catch执行此操作.这是我到目前为止:

@try {
    RNBlurModalView *modal = [[RNBlurModalView alloc] initWithViewController:self title:[NSString stringWithFormat:@"%@",entry[@"user"][@"full_name"]] message:[NSString stringWithFormat:@"%@",text[@"caption"][@"text"]]];
    [modal show];
}
@catch (NSException *exception) {
    NSLog(@"Exception:%@",exception);
}
@finally {
  //Display Alternative
}
Run Code Online (Sandbox Code Playgroud)

error-handling objective-c try-catch-finally ios

3
推荐指数
1
解决办法
2万
查看次数

编程没有"终于"

我没有支持的所需PHP版本finally,所以我想知道是否:

try {
    work();
} catch (Exception $e) {
    cleanup();
    throw $e;
}

cleanup();
Run Code Online (Sandbox Code Playgroud)

完全相同一样

try {
    work();
} finally {
    cleanup();
}
Run Code Online (Sandbox Code Playgroud)

php try-catch-finally

3
推荐指数
1
解决办法
234
查看次数

return 语句 - finally 块未正常完成

类似的问题已被问到here。但这并没有提供答案。

try {
        object = (Dev)Class.forName("Dev").newInstance();
    } catch (Exception e) 
    {
        throw new RuntimeException("Devis not available");
    }
    finally
    {
        return object;  
    }
Run Code Online (Sandbox Code Playgroud)

finally块给出警告:

finally 块没有正常完成

但根据我的理解,finally块总是被执行并返回对象。为什么警告说它不会正常完成?

java eclipse try-catch-finally

3
推荐指数
1
解决办法
8801
查看次数

Java终于返回了,奇怪的字节码

我终于看到了一个Java拼图问题,并返回

    int i = 0;
    try {
        return i;
    } finally {
        i++;
    }
Run Code Online (Sandbox Code Playgroud)

什么是这个函数的返回值,我知道这将返回0,我测试另一个代码

    StringBuffer sb = new StringBuffer("11");
    try {
        return sb;
    } finally {
        sb.append("22");
    }
Run Code Online (Sandbox Code Playgroud)

这是奇怪的事情,它返回"1122" 这是我的第一个问题:为什么它返回"1122"?

我反编译这两个java代码,

    0:   iconst_0 put 0 to the stack
1:   istore_0 store the stack top into index 0
2:   iload_0  put index 0 to the stack 
3:   istore_1 store the stack top into index 1
4:   iinc    0, 1 inc the index 0 with 1
7:   iload_1 put index 1 …
Run Code Online (Sandbox Code Playgroud)

java jvm return try-catch-finally java-bytecode-asm

3
推荐指数
1
解决办法
253
查看次数

3
推荐指数
1
解决办法
2129
查看次数

在finally和catch块中抛出异常

对catch中抛出的异常有问题,最后阻止:

class MyExc1 extends Exception {}
class MyExc2 extends Exception {}
class MyExc3 extends MyExc2 {}

public class C1 {
    public static void main(String[] args) throws Exception {
        try {
            System.out.print(1);
            q();
        }
        catch (Exception i) {
            throw new MyExc2();
        }
        finally {
            System.out.print(2);
            throw new MyExc1();
        }
    }
    static void q() throws Exception {
        try {
            throw new MyExc1();
        }
        catch (Exception y) {
            System.out.print(3);
        }
        finally {
            System.out.print(4);
            throw new Exception();
        }
    }

}
Run Code Online (Sandbox Code Playgroud)

我试过多次执行上面的代码.它每次给我不同的输出.

output 1: 1Exception …
Run Code Online (Sandbox Code Playgroud)

java exception try-catch-finally rethrow

3
推荐指数
1
解决办法
156
查看次数