可能重复:
为什么最后在C#中使用?
在C#中,有一个finally条款有什么意义?
例如.
try {
// do something
}
catch (Exception exc)
{
// do something
}
// do something
Run Code Online (Sandbox Code Playgroud)
最后代码不会执行吗?一个finally街区有什么意义?
最后使用有什么区别
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) 在我们的系统中,我们有一个抽象类,我们称之为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) 据我所知,如果您只声明一个已检查的异常,它将通过您的所有方法传播到main方法,并仍然中断您的正常程序流程,您的程序仍将停止工作.那么,为什么不总是使用try/catch处理已检查的异常...这样你的程序不会因异常而停止?为什么要在方法的签名中声明异常?对不起,我的英语不好
我试图从给定的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) 我没有支持的所需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) 类似的问题已被问到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拼图问题,并返回
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) 我想知道finally块内的代码和finally块后的代码有什么区别
对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)