Skip to content

Commit

Permalink
feature exception filters
Browse files Browse the repository at this point in the history
Add "when" as a contextual keyword.
Update grammar for exception filters
Add description of exception filters.
  • Loading branch information
BillWagner committed Jul 1, 2021
1 parent dd74dd0 commit 512b4d6
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
2 changes: 1 addition & 1 deletion standard/lexical-structure.md
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,7 @@ Contextual_Keyword
| 'get' | 'global' | 'group' | 'into' | 'join'
| 'let' | 'nameof' | 'on' | 'orderby' | 'partial'
| 'remove' | 'select' | 'set' | 'value' | 'var'
| 'where' | 'yield'
| 'when' | 'where' | 'yield'
;
```

Expand Down
13 changes: 7 additions & 6 deletions standard/statements.md
Original file line number Diff line number Diff line change
Expand Up @@ -1109,17 +1109,17 @@ There are three possible forms of `try` statements:
- A `try` block followed by a `finally` block.
- A `try` block followed by one or more `catch` blocks followed by a `finally` block.
When a `catch` clause specifies a *type*, the type shall be `System.Exception` or a type that derives from `System.Exception`. When a `catch` clause specifies a *type_parameter* it shall be a type parameter type whose effective base class is or derives from `System.Exception`.
When a `catch` clause specifies an *exception_specifier*, the type shall be `System.Exception` or a type that derives from `System.Exception`.
When a `catch` clause specifies both a *class_type* and an *Identifier*, an ***exception variable*** of the given name and type is declared. The exception variable corresponds to a local variable with a scope that extends over the `catch` block. During execution of the `catch` block, the exception variable represents the exception currently being handled. For purposes of definite assignment checking, the exception variable is considered definitely assigned in its entire scope.
When a `catch` clause specifies both a *class_type* and an *Identifier*, an ***exception variable*** of the given name and type is declared. The exception variable corresponds to a local variable with a scope that extends over the `catch` block. During execution of the *exception_filter* and `catch` block, the exception variable represents the exception currently being handled. For purposes of definite assignment checking, the exception variable is considered definitely assigned in its entire scope.
Unless a `catch` clause includes an exception variable name, it is impossible to access the exception object in the `catch` block.
Unless a `catch` clause includes an exception variable name, it is impossible to access the exception object in the filter and `catch` block.
A `catch` clause that specifies neither an exception type nor an exception variable name is called a general `catch` clause. A `try` statement can only have one general `catch` clause, and, if one is present, it shall be the last `catch` clause.
> *Note*: Some programming languages might support exceptions that are not representable as an object derived from `System.Exception`, although such exceptions could never be generated by C# code. A general `catch` clause might be used to catch such exceptions. Thus, a general `catch` clause is semantically different from one that specifies the type `System.Exception`, in that the former might also catch exceptions from other languages. *end note*
In order to locate a handler for an exception, `catch` clauses are examined in lexical order. A compile-time error occurs if a `catch` clause specifies a type that is the same as, or is derived from, a type that was specified in an earlier `catch` clause for the same try.
In order to locate a handler for an exception, `catch` clauses are examined in lexical order. If a `catch` clause specifies a type but no exception filter, it is a compile-time error for a later `catch` clause in the same `try` statement to specify a type that is the same as, or is derived from, that type. If a `catch` clause specifies no type and no filter, it shall be the last `catch` clause for that `try` statement.
> *Note*: Without this restriction, it would be possible to write unreachable `catch` clauses. *end note*
Expand Down Expand Up @@ -1176,9 +1176,10 @@ A `try` statement is executed as follows:
- If the `try` statement has a `finally` block, the `finally` block is executed.
- Control is transferred to the end point of the `try` statement.
- If an exception is propagated to the `try` statement during execution of the `try` block:
- The `catch` clauses, if any, are examined in order of appearance to locate a suitable handler for the exception. The first `catch` clause that specifies the exception type or a base type of the exception type is considered a match. A general `catch` clause is considered a match for any exception type. If a matching `catch` clause is located:
- The `catch` clauses, if any, are examined in order of appearance to locate a suitable handler for the exception. If a `catch` clause does not specify a type, or specifies the exception type or a base type of the exception type is considered a match. A general `catch` clause is considered a match for any exception type. If a matching `catch` clause is located:
- If the matching `catch` clause declares an exception variable, the exception object is assigned to the exception variable.
- Control is transferred to the matching `catch` block.
- If the `catch` clause declares an exception filter, the filter is evaluated. If it evaluates to `false`, the catch clause is not a match, and the search continues through any subsequent `catch` clauses for a suitable handler.
- Otherwise, the `catch` clause is considered a match, and control is transferred to the matching `catch` block.
- When and if control reaches the end point of the `catch` block:
- If the `try` statement has a `finally` block, the `finally` block is executed.
- Control is transferred to the end point of the `try` statement.
Expand Down

0 comments on commit 512b4d6

Please sign in to comment.