基本上遍历列表和
- 第一个对象上的Invoke方法
- 捕获第一个异常(如果有的话); 如果没有更多的例外可以捕获,请正常返回.否则,继续调用方法,直到捕获到所有异常.
- 转到下一个对象.
我可以迭代每个对象,调用方法,并捕获一个异常,但我不知道如何不断调用它上面的方法并继续捕获异常.
我在互联网上找到了一些代码如下(稍加修改).
它只是请求网页的内容.
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) 是否有以下原因无效?
@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) 我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块中,因为它超出了某些条件.
如果这样做,事实上,气味不好(我怀疑它确实如此),如何实现这一目标?
我正在开展一个项目,我需要执行两个不同的操作.我的主控制器方法中有一个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
今天我遇到了一个小问题,我有一段这样的代码,这让我有点不舒服......
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) 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块的基本功能是什么.
我有以下代码,我喜欢使用最终获得异常消息,因为通过使用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) 假设有一个类:
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) 假设我有一些像这样的C#代码:
try {
Method1();
}
catch(...) {
Method2();
}
finally {
Method3();
}
Method4();
return;
Run Code Online (Sandbox Code Playgroud)
我的问题是,只要没有异常被抛出,将方法3()方法4将()之前执行呢,还是该finally块只有之前执行return,continue或break声明?
java ×6
try-catch ×3
c# ×2
exception ×2
try-finally ×2
.net ×1
finally ×1
ios ×1
loops ×1
nsexception ×1
objective-c ×1
vb.net ×1