标签: try-catch-finally

如何创建一个try-catch块继续调用对象上的方法,直到没有更多的异常要捕获

基本上遍历列表和
- 第一个对象上的Invoke方法
- 捕获第一个异常(如果有的话); 如果没有更多的例外可以捕获,请正常返回.否则,继续调用方法,直到捕获到所有异常.
- 转到下一个对象.

我可以迭代每个对象,调用方法,并捕获一个异常,但我不知道如何不断调用它上面的方法并继续捕获异常.

java loops try-catch try-catch-finally

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

防止在Try中未赋值的变量发出警告

我在互联网上找到了一些代码如下(稍加修改).

它只是请求网页的内容.

Private Sub readWebpage(ByVal url As String)
    Dim Str As System.IO.Stream
    Dim srRead As System.IO.StreamReader
    Try
        ' make a Web request
        Dim req As System.Net.WebRequest = System.Net.WebRequest.Create(url)
        Dim resp As System.Net.WebResponse = req.GetResponse
        Str = resp.GetResponseStream
        srRead = New System.IO.StreamReader(Str)
        ' read all the text 
        textContent.text = srRead.ReadToEnd
    Catch ex As Exception
        MsgBox(ex.Message, MsgBoxStyle.Critical, "Unable to download content from: " & url)
    Finally
        srRead.Close()
        Str.Close()
    End Try
End Sub
Run Code Online (Sandbox Code Playgroud)

但是我得到两个警告:

Warning 1   Variable 'srRead' is used before it has …
Run Code Online (Sandbox Code Playgroud)

.net vb.net try-catch try-catch-finally

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

Objective-c Try/Catch没有捕获

是否有以下原因无效?

@try {
    CFGetTypeID( NULL );
}
@catch (NSException * e) {
    NSLog(@"Exception: %@", e);
}
@finally {
    NSLog(@"finally");
}
Run Code Online (Sandbox Code Playgroud)

try/catch问题类似,只是看起来上面的块每次都崩溃了.我知道我的调试器设置正确,因为我从另一个问题设置了一个try/catch:

// Test working try catch
NSString* test = [NSString stringWithString:@"ss"];

@try {
    [test characterAtIndex:6];
}
@catch (NSException * e) {
    NSLog(@"Exception: %@", e);
}
@finally {
    NSLog(@"finally");
}

// Now test NULL entry
@try {
    CFGetTypeID( NULL );
}
@catch (NSException * e) {
    NSLog(@"Exception: %@", e);
}
@finally {
    NSLog(@"finally");
}
Run Code Online (Sandbox Code Playgroud)

objective-c try-catch-finally nsexception ios

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

尝试/捕获/最后,在最后使用Catch中的异常?

try catch finally在这里看了几个其他的问题,但我不确定这个问题是否得到了解答.做以下事情是不是很糟糕:

Exception? ex = null;
try { //something }
catch (Exception e) { ex = e ;}
finally {
    DoSomething();        
}
... //more code

//end of method
if (ex !=null) { throw ex; }
Run Code Online (Sandbox Code Playgroud)

基本上,我正在尝试确保运行某些代码(在try/catch/finally之外)并且如果发生异常则抛出异常,但直到所述代码运行之后才会抛出异常.我无法将所有代码放在finally块中,因为它超出了某些条件.

如果这样做,事实上,气味不好(我怀疑它确实如此),如何实现这一目标?

c# try-catch-finally

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

我可以在课堂上有两个以上的块

我正在开展一个项目,我需要执行两个不同的操作.我的主控制器方法中有一个finally块.

我的问题是,我最终可以有两个以上,例如:

class test
{
    X()
    {
        try
        {
            //some operations
        }
        finally
        {
            // some essential operation
        }

    }

    //another method
    Y()
    {
        try
        {
            //some operations
        }
        finally
        {
            // some another essential operation
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

那么,有可能吗?

java exception-handling exception try-catch try-catch-finally

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

在java中使用try-finally的好习惯?

今天我遇到了一个小问题,我有一段这样的代码,这让我有点不舒服......

   try{
       //stuff...
   } finally {
       //finally stuff
   }
Run Code Online (Sandbox Code Playgroud)

我想知道这样的实现是否是一个很好的做法,在何时发生异常try并被重新抛给处理它的调用者.这相当于下面的代码吗?

    try{
        //code....
    } catch (Exception e) {
        //handling...
    } finally {
        //finishing up
    }
Run Code Online (Sandbox Code Playgroud)

java try-catch-finally try-finally

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

"最终"块的功能是什么?

class Demo {
    public static void main(String args[]) {
        System.out.println("Start main");
        try {
            //exceptional code
            int x=43/0;
        } catch(ArithmeticException e) {
            e.printStackTrace();
        } finally {
            System.out.println("final code");
        }
        System.out.println("End main");
    }
}
Run Code Online (Sandbox Code Playgroud)

我使用上面的代码来理解finally块的机制.在这种情况下,我观​​察到的是,即使没有该catch块,finally块也会执行并显示发生的异常.但我观察到的差异是,当不使用catch块时,不会打印"End main".我想知道finally块执行的原因,即使未使用catch子句处理异常.我想知道finally块的基本功能是什么.

java try-catch-finally

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

使用java中的finally块捕获其消息的异常

我有以下代码,我喜欢使用最终获得异常消息,因为通过使用catch我可以很容易地通过它的arg.but我知道我无法使用finally获取异常消息.

try {
 MyClass obj=new MyClass();
 obj.strProName = jobj1.getString("productname");
 obj.strPrice = jobj1.getString("price");
 obj.strCurrency = jobj1.getString("currency");
 obj.strSalePrice = jobj1.getString("saleprice");
 obj.strStoreName = jobj1.getString("storename");

//arrayList.add(obj);
throw new Exception("Exception Reason!");

}
finally{
 //want to get that exception message here without using catch or can see how finally catching here the exception
}
Run Code Online (Sandbox Code Playgroud)

java exception-handling try-catch-finally

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

Java未处理的异常消失

假设有一个类:

public class Magic {

public static void main(String[] args){
    boolean[] val = new boolean[0];
    paradox(val);
}

private static boolean paradox(boolean[] arg) {
    Container container = null;
    try {
        container = Container.getContainer(arg[0]);
        return container.value;
    } catch (UnsupportedOperationException e) {
        e.printStackTrace();
        return false;
    } finally {
        try {
            container.sayHi();
        } catch (UnsupportedOperationException e) {
            e.printStackTrace();
        }
    }
}

private static class Container{
    private boolean value;

    private Container(boolean value){
        this.value = value;
    }

    private static Container getContainer(boolean value) throws UnsupportedOperationException{
        return new …
Run Code Online (Sandbox Code Playgroud)

java exception try-catch-finally

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

try,catch和finally块的执行顺序

假设我有一些像这样的C#代码:

try {
    Method1();
}
catch(...) {
    Method2();
}
finally {
    Method3();
}
Method4();
return;
Run Code Online (Sandbox Code Playgroud)

我的问题是,只要没有异常被抛出,将方法3()方法4将()之前执行呢,还是该finally块只有之前执行return,continuebreak声明?

c# exception-handling finally try-catch-finally try-finally

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