Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions docs/csharp/language-reference/keywords/try-catch.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,19 @@ catch (InvalidCastException e)
{
// Take some action.
}
}
}
```


> [!NOTE]
> It is also possible to use a predicate expression to get a similar result in a often cleaner fashion. The following example has a similar behavior for callers than the precedent example, the function throws the `InvalidCastException` back to the caller when `e.Data` is null.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of "predicate expression", I'd prefer the term "exception filter".

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some additional suggested changes, @sylveon:

  • a often -->an often
  • than the precedent example, --> as the previous example.
  • the function --> The function
  • null --> `null`

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was following the name used a bit before in the same document.

Using catch arguments is one way to filter for the exceptions you
want to handle. You can also use a predicate expression that further
examines the exception to decide whether to handle it. If the
predicate expression returns false, then the search for a handler continues.

Should I change that too while I'm at it @BillWagner?

@rpetrusha will do.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks @sylveon Yes, let's change it in both locations

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done and done.

>
> ```csharp
> catch (InvalidCastException e) when (e.Data != null)
> {
> // Take some action.
> }
> ```

From inside a `try` block, initialize only variables that are declared therein. Otherwise, an exception can occur before the execution of the block is completed. For example, in the following code example, the variable `n` is initialized inside the `try` block. An attempt to use this variable outside the `try` block in the `Write(n)` statement will generate a compiler error.

```csharp
Expand Down