Exception handling is required in any application. It is a very interesting issue where different apps have various way(s) to handle that. I plan to write a series of articles to discuss this issue
- Exception Handling (1), in ASP.NET MVC
- Exception Handling (2), in ASP.NET Web API
- Exception Handling (3), in ASP.NET Core MVC
- Exception Handling (4), in ASP.NET Core Web API
- Exception Handling (5), in ASP.NET Summary
- Exception Handling (6), HttpStatusCode
- Exception Handling (7), C# Exception Handling Statements --- this article
respectively.
This article will discuss C# Exception Handling Statements: try-catch-finally and throw keywords.
Introduction
The Exception Handling Statements are
- throw
- try-catch
- try-finally
- try-catch-finally
Since the first article of my series on Exception Handling, I have mentioned try-catch-finally as one of the methods. However, we did not discuss them in detail because everybody knows them. In this article, we will not discuss the exception statements in general but discuss some features we might miss.
- A - Valid Formats for try-catch-finally Statements
- B - The case: try is not handled by catch
- C - Difference between throw and throw ex
Note:
We will use the code introduced in Exception Handling (1), in ASP.NET MVC, especially using Step 3 - Error handling by Try-Catch, index5.
A - Valid Formats for try-catch-finally Statements
We are very familiar with try-catch-finally Statements, so I will give a short definition:
- The try statement identifies a block of statements within which an exception might be thrown.
- The catch statement must be associated with a
trystatement and identifies a block of statements that can handle a particular exception type. The statements are executed if an exception of a particular type occurs within thetryblock. - The finally statement must be associated with a
trystatement and identifies a block of statements that are executed regardless of whether or not an error occurs within thetryblock.
We realize that the catch or finally statement must be associated with a try. Therefore we have the following valid formats for the try-catch-finally Statements will be:
- try-catch
- try-finally
- try-catch-finally
While in the following examples, they are not valid:
Using try only, it will not be compilable with the error message: Expect catch or finally:

Catch only: Syntax error, 'try' expected.

The same is true for finally:

B - The case: try is not handled by catch
As we know, every try must be associated with at least a catch or finally statement,
- In the case of try-finally, without a catch, the try will go to the finally for sure, however,
- In the case of try-catch(-finally), if the catch does not fit the exception type, the exception will not be caught, the workflow likes this, and the catch is skipped.










Join the conversation! Your thoughts help the community grow.