在采访中,采访者向我询问..我有一个代码,写在try和catch块之类的
try
{
//code line 1
//code line 2
//code line3 -- if error occur on this line then did not go in the catch block
//code line 4
//code line 5
}
catch()
{
throw
}
Run Code Online (Sandbox Code Playgroud)
假设我们在代码行3上出现错误,那么这不会进入catch块但是如果我在除了第3行之外的任何其他行上都有错误它会进入catch块
这是否可能,如果在特定行上发生错误,那么它不会进入catch块?
您可以将第 3 行换行到另一个try/catch块中:
try
{
//code line 1
//code line 2
try
{
//code line3 -- if error occur on this line then did not go in the catch block
}
catch { }
//code line 4
//code line 5
}
catch()
{
throw;
}
Run Code Online (Sandbox Code Playgroud)
面试官还必须定义错误。正在谈论异常,因为错误可能意味着很多事情 => 糟糕的代码、异常、不按预期的代码运行,...