C# throw

In the previous sections, the exceptions are thrown by CLR. In this section, we'll use the throw keyword to throw an exception explicitly in a statement.

There are 3 different ways to use the throw statement.

throw;				// Rethrow the caught exception and the stack trace is not changed.
throw e;			// Throw the exception e and reset the stack trace;
throw new <Exception>();	// Throw a new <Exception>. The <Exception> can be any exception class.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
using System;

public class Program
{
    static void Main()
    {
        testThrow(1);
        testThrow(2);
        testThrow(3);

        Console.Read();
    }

    static void testThrow(int i)
    {
        try
        {
            switch (i)
            {
                case 1:
                    test1();
                    break;
                case 2:
                    test2();
                    break;
                case 3:
                    test3();
                    break;
            }
        }
        catch (DivideByZeroException e)
        {
            Console.WriteLine("Caught in DivideByZeroException.");
            Console.WriteLine(e.StackTrace);
        }
        catch (ApplicationException e)
        {
            Console.WriteLine("Caught in ApplicationException.");
            Console.WriteLine(e.StackTrace);
        }
        catch (Exception e)
        {
            Console.WriteLine("Caught in Exception.");
            Console.WriteLine(e.StackTrace);
        }
        finally
        {
            Console.WriteLine();
        }
    }

    static void test1()
    {
        try
        {
            DivByZero();
        }
        catch (DivideByZeroException e)
        {
            throw;
        }
    }

    static void test2()
    {
        try
        {
            DivByZero();
        }
        catch (Exception e)
        {
            throw e;
        }
    }

    static void test3()
    {
        try
        {
            DivByZero();
        }
        catch (Exception)
        {
            throw new ApplicationException();
        }
    }

    private static void DivByZero()
    {
        int a = 0;
        int b = 1 / a;  // DivideByZeroException is thrown
    }
}

Output

Caught in DivideByZeroException.
   at Program.DivByZero() in d:\tests\HelloWorld\HelloWorld\Program.cs:line 91
   at Program.test1() in d:\tests\HelloWorld\HelloWorld\Program.cs:line 60
   at Program.testThrow(Int32 i) in d:\tests\HelloWorld\HelloWorld\Program.cs:line 21

Caught in DivideByZeroException.
   at Program.test2() in d:\tests\HelloWorld\HelloWorld\Program.cs:line 72
   at Program.testThrow(Int32 i) in d:\tests\HelloWorld\HelloWorld\Program.cs:line 24

Caught in ApplicationException.
   at Program.test3() in d:\tests\HelloWorld\HelloWorld\Program.cs:line 84
   at Program.testThrow(Int32 i) in d:\tests\HelloWorld\HelloWorld\Program.cs:line 27

Explanation

  • Line 7-9: Test throw statement in 3 different ways.
  • Line 18-29: Based on the parameter, the switch statement is used to call 3 different methods.
  • Line 31-45: Catch clauses and output the StackTrace property of the caught exception.
  • Line 46: finally statement is used to output a new line.
  • Line 60: The caught exception is rethrown without changing the stack trace. The exception starts at line 91.
  • Line 72: The exception e is thrown but the stack trace is changed. The exception starts at this line instead of line 91.
  • Line 84: A new exception is thrown and the stack trace starts at this line. The old exception DivideByZeroException is caught and handled here.
  • Line 91: The original DivideByZeroException is thrown here.