c# exception handling best practices code example
Example 1: exception handling c#
try {
// statements causing exception
} catch( ExceptionName e1 ) {
// error handling code
} catch( ExceptionName e2 ) {
// error handling code
} catch( ExceptionName eN ) {
// error handling code
} finally {
// statements to be executed
}
Example 2: exception handling in c# web api
catch (Exception ex)
{
_logger.LogError(ex.ToString());
Response.AddApplicationError(ex.Message);
return StatusCode(500, ex.Message);
}