异常处理,需要帮助 [英] Exception handling, need help

查看:74
本文介绍了异常处理,需要帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有方法1和方法2没有尝试捕获,方法3有try catch。考虑以下代码。



 Method1()
{
Method2()
'做一些东西
}

方法2()
{
方法3()
'做一些东西
}
方法3()
{
尝试
赶上ex Exception
throw ex
end try
}





如果在方法3中发生异常抛出ex,则在方法2中调用方法3之后的代码ll不会被执行也不会从方法1执行。



现在我需要的是在Method2中处理异常。考虑以下。



 Method1()
{
Method2()
'做一些事情
}

方法2()
{
尝试
method3()
'做一些事情
赶上ex Exception
messagebox.show()
end try
}
Method3()
{
try
catch ex as Exception
抛出前
结束尝试
}



现在在这种情况下,来自method3的异常将在方法2中处理但在处理该异常之后我想停止执行调用方法2的方法1中的代码。我不想在方法1中改变任何东西,因为它已经从其他几千个地方调用过。 :)

我可以实现这个目标。请帮助

解决方案

在方法1中放置try catch并从方法2中重新抛出错误。这将是最简单的方法。


如果我理解正确,那么你需要的是在方法2中重新抛出异常,就像在catch子句中的Method3中一样。

 messagebox.show ( ); 
throw ex;



如果你这样做,那么Method1中的剩余代码将不会被执行,它将在搜索异常处理程序时被绕过。

如果你不希望你的代码因未处理的异常错误而崩溃,那么你需要在堆栈中的最后调用Method1的异常处理程序,以便异常将获得陷入了某个地方。



一般来说,这看起来不是一个理想的解决方案。像这样的复杂控制流程具有导致内存泄漏的主要趋势,尤其是涉及外部处理时。如果这些例外案例实际上是系统如何工作的设计的一部分,那么它们实际上并非如此并且应该以另一种方式进行编码。


Hi, I have method 1 and Method 2 without try catch and Method 3 with try catch. consider following code.

Method1()
{
Method2()
' doing some stuff
}

Method2()
{
method3()
' doing some stuff
}
Method3()
{
try
catch ex as Exception
throw ex
end try 
}



if Exception occurred in Method 3 on throwing ex, code after from where method 3 is called in method2 ll not get executed also ll not get executed from method1.

Now what I need is to handle exception in Method2. consider following.

Method1( )
{
Method2( )
' doing some stuff
}

Method2( )
{
try
method3( )
' doing some stuff
catch ex as Exception
messagebox.show("")
end try
}
Method3( )
{
try
catch ex as Exception
throw ex
end try 
}  


Now in this case exception from method3 ll be handle in method2 but after handling that exception I Want to stop executing code in method1 from where method 2 is called. i dont want to change anything in Method 1, because its been called from thousand of other places. :)
i can I achieve this. Please help

解决方案

Put a try catch in method 1 and re-throw the error from method 2. would be the simplest way to do it.


If I understand you correctly then what you need is to rethrow the exception in Method2 just as you do in Method3 within the catch clause.

messagebox.show("");
throw ex;


If you do this then the remaining code in Method1 will not get executed, it wil get bypassed in the search for an exception handler.
If you don''t want your code to crash wih an unhandled exception fault you''ll need to have an exception handler further down the stack in something that ultimately calls Method1 so that the exception will get caught somewhere.

Generally though this is not looking like an ideal solution. Complex flow of control like this has a major tendency to lead to memory leaks, especially when excepton handling is involved. If these ''exceptional'' cases are in fact part of the design of how the system is intended to work then they aren''t really exceptional and should be coded for in another way.


这篇关于异常处理,需要帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆