Skip to content

Move samples used in Catch exception section to snippets folder #24084

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Aug 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ When an exception occurs, it is passed up the stack and each catch block is give

The following code example uses a `try`/`catch` block to catch an <xref:System.InvalidCastException>. The sample creates a class called `Employee` with a single property, employee level (`Emlevel`). A method, `PromoteEmployee`, takes an object and increments the employee level. An <xref:System.InvalidCastException> occurs when a <xref:System.DateTime> instance is passed to the `PromoteEmployee` method.

[!code-cpp[CatchException#2](../../../samples/snippets/cpp/VS_Snippets_CLR/CatchException/CPP/catchexception1.cpp#2)]
[!code-csharp[CatchException#2](../../../samples/snippets/csharp/VS_Snippets_CLR/CatchException/CS/catchexception1.cs#2)]
[!code-vb[CatchException#2](../../../samples/snippets/visualbasic/VS_Snippets_CLR/CatchException/VB/catchexception1.vb#2)]
:::code language="cpp" source="./snippets/how-to-use-specific-exceptions-in-a-catch-block/cpp/catchexception.cpp" id="Snippet2":::
:::code language="csharp" source="./snippets/how-to-use-specific-exceptions-in-a-catch-block/csharp/catchexception.cs" id="Snippet2":::
:::code language="vb" source="./snippets/how-to-use-specific-exceptions-in-a-catch-block/vb/catchexception.vb" id="Snippet2":::

## See also

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ ms.date: "02/06/2019"
dev_langs:
- "csharp"
- "vb"
- "cpp"
helpviewer_keywords:
- "exceptions, try/catch blocks"
- "try blocks"
Expand All @@ -18,8 +19,9 @@ Place any code statements that might raise or throw an exception in a `try` bloc

In the following example, a <xref:System.IO.StreamReader> opens a file called *data.txt* and retrieves a line from the file. Since the code might throw any of three exceptions, it's placed in a `try` block. Three `catch` blocks catch the exceptions and handle them by displaying the results to the console.

[!code-csharp[CatchException#3](~/samples/snippets/csharp/VS_Snippets_CLR/CatchException/CS/catchexception2.cs#3)]
[!code-vb[CatchException#3](~/samples/snippets/visualbasic/VS_Snippets_CLR/CatchException/VB/catchexception2.vb#3)]
:::code language="cpp" source="./snippets/how-to-use-the-try-catch-block-to-catch-exceptions/cpp/catchexception.cpp" id="Snippet3":::
Copy link
Member

Choose a reason for hiding this comment

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

Thank you for adding C++ here, much appreciated.

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've seen it while preparing the move of the other page samples.
I also left some unused files in the samples folder, I did not find any usage of them, but did not want to delete them anyway.

:::code language="csharp" source="./snippets/how-to-use-the-try-catch-block-to-catch-exceptions/csharp/catchexception.cs" id="Snippet3":::
:::code language="vb" source="./snippets/how-to-use-the-try-catch-block-to-catch-exceptions/vb/catchexception.vb" id="Snippet3":::

The Common Language Runtime (CLR) catches exceptions not handled by `catch` blocks. If an exception is caught by the CLR, one of the following results may occur depending on your CLR configuration:

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public static void PromoteEmployee(Object emp)
e.Emlevel = e.Emlevel + 1;
}

public static void Main()
static void Main()
{
try
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public ref class ProcessFile
{
StreamReader^ sr = File::OpenText("data.txt");
Console::WriteLine("The first line of this file is {0}", sr->ReadLine());
sr->Close();
sr->Close();
}
catch (Exception^ e)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>

</Project>