-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExceptionHandlingExample.cs
More file actions
197 lines (178 loc) · 6.1 KB
/
Copy pathExceptionHandlingExample.cs
File metadata and controls
197 lines (178 loc) · 6.1 KB
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
using System;
using UdonSharp;
using UnityEngine;
namespace LogicCuteGuy.LCGUdonSharp.Examples.ExtendedLanguage
{
[UdonBehaviourSyncMode(BehaviourSyncMode.NoVariableSync)]
public class ExceptionHandlingExample : UdonSharpBehaviour
{
private const string LogPrefix = "[try/catch] ";
public int result;
public int finallyCount;
public string message;
public int code;
public string operation;
public UdonExceptionKind kind;
public bool argumentCaught;
public bool arrayCaught;
public bool stringCaught;
public bool divideCaught;
public bool moduloCaught;
public bool nullCaught;
public bool negativeWriteCaught;
public bool rethrowCaught;
public bool finallyOverrideCaught;
public int sideEffectCount;
public override void Interact()
{
result = 0;
finallyCount = 0;
sideEffectCount = 0;
argumentCaught = false;
arrayCaught = false;
stringCaught = false;
divideCaught = false;
moduloCaught = false;
nullCaught = false;
negativeWriteCaught = false;
rethrowCaught = false;
finallyOverrideCaught = false;
try
{
ThrowArgumentNull();
result = -1;
}
catch (ArgumentException exception)
{
message = exception.Message;
argumentCaught = true;
result = 10;
Debug.Log(LogPrefix + "Caught ArgumentException: " + exception.Message);
}
finally
{
finallyCount++;
result++;
Debug.Log(LogPrefix + "The first finally block ran.");
}
try
{
throw new UdonException(message: "explicit payload", kind: UdonExceptionKind.InvalidOperation,
operation: "demo", code: 17);
}
catch (UdonException exception)
{
message = exception.Message;
kind = exception.Kind;
code = exception.Code;
operation = exception.Operation;
result += exception.Code;
Debug.Log(LogPrefix + "Caught UdonException: " + exception.Message);
Debug.Log(LogPrefix + "Kind=" + (int)exception.Kind + ", Code=" + exception.Code +
", Operation=" + exception.Operation);
}
int[] values = { 1 };
try { result += values[2]; }
catch (IndexOutOfRangeException)
{
arrayCaught = true;
result += 100;
Debug.Log(LogPrefix + "Caught an array upper-bound read.");
}
string text = "ok";
try { result += text[3]; }
catch (IndexOutOfRangeException)
{
stringCaught = true;
result += 1000;
Debug.Log(LogPrefix + "Caught a string upper-bound read.");
}
int divisor = 0;
try { result += 10 / divisor; }
catch (DivideByZeroException)
{
divideCaught = true;
result += 10000;
Debug.Log(LogPrefix + "Caught integral division by zero.");
}
try { result += 10 % divisor; }
catch (DivideByZeroException)
{
moduloCaught = true;
result += 1000000000;
Debug.Log(LogPrefix + "Caught integral modulo by zero.");
}
string missing = null;
try { result += missing.Length; }
catch (NullReferenceException)
{
nullCaught = true;
result += 10000000;
Debug.Log(LogPrefix + "Caught a null receiver.");
}
try { values[NextBadIndex()] = 4; }
catch (IndexOutOfRangeException)
{
negativeWriteCaught = true;
result += 100000000;
Debug.Log(LogPrefix + "Caught a negative array write index.");
}
try
{
try { throw new NotSupportedException("rethrow payload"); }
catch (NotSupportedException) { throw; }
}
catch (NotSupportedException)
{
rethrowCaught = true;
result += 100000;
Debug.Log(LogPrefix + "Caught a rethrown exception.");
}
try
{
try { throw new ArgumentException("superseded payload"); }
finally { throw new InvalidOperationException("finally payload"); }
}
catch (InvalidOperationException)
{
finallyOverrideCaught = true;
result += 1000000;
Debug.Log(LogPrefix + "The exception from finally replaced the earlier exception.");
}
result += ReturnThroughFinally();
for (int i = 0; i < 2; i++)
{
try
{
if (i == 0)
continue;
break;
}
finally
{
finallyCount += 100;
}
}
Debug.Log(LogPrefix + "Completed. Result=" + result + ", FinallyCount=" + finallyCount);
}
private void ThrowArgumentNull()
{
throw new ArgumentNullException("compiler-managed argument failure");
}
private int NextBadIndex()
{
sideEffectCount++;
return -1;
}
private int ReturnThroughFinally()
{
try { return 7; }
finally { finallyCount += 10; }
}
public void ThrowUncaught()
{
throw new UdonException(UdonExceptionKind.InvalidOperation,
"uncaught compiler-managed failure", 91, "uncaught-demo");
}
}
}