On Error catch block did not call
In an Interview,Interviewe开发者_Go百科r asked to me.. that I have a code which written in side the try and catch block like
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
}
suppose we got an error on code line 3 then this will not go in the catch block but If I got error on any other line except line 3 it go in catch block
is this possible that if error occur on a particular line then it is not go in the catch block?
You could wrap line 3 in another try/catch
block:
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;
}
Also the interviewer must have defined error. Was talking about an exception as an error could mean many things => crappy code, exception, not behaving as expected code, ...
If you line 3 causes non CLS-compliant exceptions
, it won't be catch'ed with parameterized catch() block. To catch all type of exceptions, use parameterless catch block.
try
{
// Statement which causes an exception
}
catch //No parameters
{
//Handles any type of exception
}
.net Exception catch block
Short Answer: Yes
There are errors that a catch block will not get. I think out of memory error. Another way an Exception can skip a block is if the error thrown is not one of the ones you defined.
精彩评论