假设我需要运行 methodA,但是 methodA 将抛出 FormatException 异常。
如果我写了下面这段代码块:
如果我写了下面这段代码块:
try
{
methodA();
}
catch (Exception ex)
{
methodB();
}
catch (FormatException ex)
{
methodC();
}
如果知道FormatException也是一种异常,因此会进入methodB的catch块中,那么它是否仍会运行methodC呢?
还是更好地这样写:
try
{
methodA();
}
catch (Exception ex)
{
if(ex is FormatException)
{
methodC();
} else
{
methodB();
}
}