Answer: If an exception occurs inside a try block, then the control flow passes immediately to the next catch statement. When control flow passes to a catch block, the statements contained in the catch block are processed to handle the exception.
Example:
int SafeDivision(int x, int y)
{
try
{
return (x / y);
}
catch (System.DivideByZeroException dbz)
{
System.Console.WriteLine("Division by zero attempted!");
return 0;
}
}
Asked In: Many Interviews |
Alert Moderator