summaryrefslogtreecommitdiff
path: root/tests/src/JIT/Methodical/eh/finallyexec/tryfinallythrow_nonlocalexit.cs
blob: 5391ec5dada59de9a982882172e3e807d5444800 (plain)
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
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.

/*
A test to make sure that calls to finally for nonlocal exits are
done outside of trybody 
    try {
        goto nonlocal_exit;
    } finally {
        throw; // if the finally is being called from trybody, the finally will be executed 
           // multiple times
    }
*/
using System;

namespace hello
{
    class Class1
    {

        private static TestUtil.TestLog testLog;

        static Class1()
        {
            // Create test writer object to hold expected output
            System.IO.StringWriter expectedOut = new System.IO.StringWriter();

            // Write expected output to string writer object
            expectedOut.WriteLine("In main's try");
            expectedOut.WriteLine("in finally");
            expectedOut.WriteLine("caught in main");
            expectedOut.WriteLine("Passed");

            // Create and initialize test log object
            testLog = new TestUtil.TestLog(expectedOut);
        }

        static public void Middle(int i)
        {
            try
            {
                if (i == 0) goto done;
                Console.WriteLine("in try");
            }
            finally
            {
                Console.WriteLine("in finally");
                if (i == 0) throw new Exception();
            }
            Console.WriteLine("after finally");
            done:
            Console.WriteLine("done");

        }

        static public int Main(string[] args)
        {
            //Start recording
            testLog.StartRecording();
            try
            {
                Console.WriteLine("In main's try");
                Middle(args.Length);
            }
            catch
            {
                Console.WriteLine("caught in main");
            }
            Console.WriteLine("Passed");

            // stop recoding
            testLog.StopRecording();

            return testLog.VerifyOutput();
        }
    }
}