.NET Framework 4
How to: Explicitly Throw Exceptions
You can explicitly throw an exception using the throw 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.
The following code example uses a try/catch block to catch a possible FileNotFoundException. Following the try block is a catch block that catches the FileNotFoundExceptionand writes a message to the console if the data file is not found. The next statement is the throw statement that throws a new FileNotFoundException and adds text information to the exception.
Example
Visual Basic
Option Strict On Imports System Imports System.IO Public Class ProcessFile Public Shared Sub Main() Dim fs As FileStream = Nothing Try 'Opens a text file. fs = New FileStream("c:\temp\data.txt", FileMode.Open) Dim sr As New StreamReader(fs) Dim line As String 'A value is read from the file and output to the console. line = sr.ReadLine() Console.WriteLine(line) Catch e As FileNotFoundException Console.WriteLine("[Data File Missing] {0}", e) Throw New FileNotFoundException("[data.txt not in c:\temp directory]", e) Finally If fs IsNot Nothing Then fs.Close End Try End Sub End Class
C#
using System; using System.IO; public class ProcessFile { public static void Main() { FileStream fs = null; try { //Opens a text tile. fs = new FileStream(@"C:\temp\data.txt", FileMode.Open); StreamReader sr = new StreamReader(fs); string line; //A value is read from the file and output to the console. line = sr.ReadLine(); Console.WriteLine(line); } catch(FileNotFoundException e) { Console.WriteLine("[Data File Missing] {0}", e); throw new FileNotFoundException(@"[data.txt not in c:\temp directory]",e); } finally { if (fs != null) fs.Close(); } } }
See Also
Tasks
Other Resources
Community Content
Stefan Hoffmann
Throwing exceptions
It is important to throw or rethrow an exception only in either the catch or the finally block. Otherwise the exception thrown in the finally block will hide the exception thrown in the catch block. E.g.:
internal class Program
{
static void Main(string[] args)
{
try
{
try
{
Console.WriteLine("inner try");
throw new Exception("inner try exception");
}
catch
{
Console.WriteLine("inner catch");
throw new Exception("inner catch exception");
}
finally
{
Console.WriteLine("inner finally");
// Try it twice, once without and once with throwing the exception below.
// throw new Exception("inner finally exception");
}
}
catch (Exception exception)
{
Console.WriteLine("outer catch: " + exception.Message);
}
}
}
internal class Program
{
static void Main(string[] args)
{
try
{
try
{
Console.WriteLine("inner try");
throw new Exception("inner try exception");
}
catch
{
Console.WriteLine("inner catch");
throw new Exception("inner catch exception");
}
finally
{
Console.WriteLine("inner finally");
// Try it twice, once without and once with throwing the exception below.
// throw new Exception("inner finally exception");
}
}
catch (Exception exception)
{
Console.WriteLine("outer catch: " + exception.Message);
}
}
}