Skip to content
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 @@ -15,7 +15,7 @@ ms.assetid: 72bdd157-caa9-4478-9ee3-cb4500b84528
---
# How to explicitly throw exceptions

You can explicitly throw an exception using the C# [`throw`](../../csharp/language-reference/keywords/throw.md) or the Visual Basic [`Throw`](../../visual-basic/language-reference/statements/throw-statement.md) statement. You can also throw a caught exception again using the `throw` statement. It is good coding practice to add information to an exception that is re-thrown to provide more information when debugging.
You can explicitly throw an exception using the C# [`throw`](../../csharp/language-reference/keywords/throw.md) or the Visual Basic [`Throw`](../../visual-basic/language-reference/statements/throw-statement.md) statement. You can also throw a caught exception again using the `throw` statement. It's good coding practice to add information to an exception that's rethrown to provide more information when debugging.

The following code example uses a `try`/`catch` block to catch a possible <xref:System.IO.FileNotFoundException>. Following the `try` block is a `catch` block that catches the <xref:System.IO.FileNotFoundException> and writes a message to the console if the data file is not found. The next statement is the `throw` statement that throws a new <xref:System.IO.FileNotFoundException> and adds text information to the exception.

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

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<Nullable>enable</Nullable>
</PropertyGroup>

</Project>

Original file line number Diff line number Diff line change
@@ -1,32 +1,35 @@
// <Snippet1>
using System;
using System;
using System.IO;

public class ProcessFile
namespace Example
{
public static void Main()
public class ProcessFile
{
FileStream fs;
try
public static void Main()
{
// Opens a text tile.
fs = new FileStream(@"C:\temp\data.txt", FileMode.Open);
var sr = new StreamReader(fs);
// <Snippet1>
var fs = default(FileStream);
try
{
// Opens a text tile.
fs = new FileStream(@"C:\temp\data.txt", FileMode.Open);
var sr = new StreamReader(fs);

// A value is read from the file and output to the console.
string line = sr.ReadLine();
Console.WriteLine(line);
}
catch(FileNotFoundException e)
{
Console.WriteLine($"[Data File Missing] {e}");
throw new FileNotFoundException(@"[data.txt not in c:\temp directory]", e);
}
finally
{
if (fs != null)
fs.Close();
// A value is read from the file and output to the console.
string? line = sr.ReadLine();
Console.WriteLine(line);
}
catch (FileNotFoundException e)
{
Console.WriteLine($"[Data File Missing] {e}");
throw new FileNotFoundException(@"[data.txt not in c:\temp directory]", e);
}
finally
{
if (fs != null)
fs.Close();
}
// </Snippet1>
}
}
}
// </Snippet1>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Library</OutputType>
<TargetFrameworks>net7.0</TargetFrameworks>
</PropertyGroup>

</Project>

Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Imports System.IO
Public Class ProcessFile

Public Shared Sub Main()
Dim fs As FileStream
Dim fs As FileStream = Nothing
Try
' Opens a text file.
fs = New FileStream("c:\temp\data.txt", FileMode.Open)
Expand Down