如何检查某些嵌套方法是否引起异常并进行处理
本文关键字:是否 异常 处理 方法 嵌套 何检查 检查 | 更新日期: 2023-09-27 18:13:44
我有一个代码像-
void OuterMethod()
{
InnerMethod();
//Some way to know that InnerMethod() have some handled exception.
}
void InnerMethod()
{
try
{
//Some Exception Thrown.
}
catch(Exception ex)
{
//Exception Handled.
}
}
现在,当我调用InnerMethod()有一个处理异常在它。问题是,我需要知道它在OuterMethod()中,我调用InnerMethod()。
注:-我不能改变返回类型,因为它是一个巨大的已经写了许多嵌套方法的代码。方法被多次引用
您可以在内部方法中再次抛出它,并在外部
中捕获它void OuterMethod()
{
try
{
InnerMethod();
}
catch(Exception ex)
{
//Exception Handled.
}
}
void InnerMethod()
{
try
{
//Some Exception Thrown.
}
catch(Exception ex)
{
//Exception Handled.
//some logic
throw;
}
}
您可以执行以下操作
void OuterMethod()
{
try
{
InnerMethod();
}
catch(Exception ex)
{
//Handle Exception
}
}
void InnerMethod()
{
//if exception occurs, throw
}