-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
217 lines (199 loc) · 7.16 KB
/
Program.cs
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
using System;
using System.Collections.Generic;
namespace ExceptionHandling
{
class Account
{
public string FirstName { get; private set; }
public string LastName { get; private set; }
public int Balance { get; private set; }
public Account(string firstName, string lastName, int balance)
{
FirstName = firstName;
LastName = lastName;
Balance = balance;
}
public void Withdraw(int amount)
{
if (amount > Balance)
{
throw new InvalidOperationException("Insufficient fund");
}
Balance = Balance - amount;
}
}
class Student
{
public string Name { get; set; }
}
class Program
{
// StackOverflowError
static void RecursiveMethod(int i)
{
i++;
RecursiveMethod(i);
}
public static void Main()
{
//NullRference Example
try
{
/* NullRference occurs here as array of Student objects
* are created but not intialised. trying to access its Name
* property results in NullReference expection.
*/
Student[] students = new Student[3];
Console.WriteLine(students[0].Name);
}
catch (NullReferenceException exception)
{
Console.WriteLine("The following error detected: " +
exception.GetType().ToString() + " with message \"" +
exception.Message + "\"");
}
//indexOutOfrange Example
try
{
int[] myArray = {0, 1, 2, 3, 4};
/* Here the loop is trying acces the element at the
* index greater than the size of array.
*/
for(int i = 0; i <= myArray.Length; i++)
{
Console.Write(myArray[i] + "\t");
}
Console.WriteLine();
}
catch (IndexOutOfRangeException exception)
{
Console.WriteLine("The following error detected: " +
exception.GetType().ToString() + " with message \"" +
exception.Message + "\"");
}
// OutOfMemoryException Example
try
{
List<string[]> myList = new List<string[]>();
while(true)
{
string[] names = new string[int.MaxValue];
myList.Add(names);
}
}
catch(OutOfMemoryException exception)
{
Console.WriteLine("The following error detected: " +
exception.GetType().ToString() + " with message \"" +
exception.Message + "\"");
}
//InvalidCastException example
object myObject = "Police";
try
{
/* trying to cast a string object to an integer
* using the (int) cast operator. Since the string cannot be
* converted to an integer, an InvalidCastException is thrown.
*/
int cast = (int)myObject;
}
catch (InvalidCastException exception)
{
Console.WriteLine("The following error detected: " +
exception.GetType().ToString() + " with message \"" +
exception.Message + "\"");
}
// DivideByZeroException Example
try
{
int a = 6;
int b = 0;
Console.WriteLine(a / b);
}
catch (DivideByZeroException exception)
{
Console.WriteLine("The following error detected: " +
exception.GetType().ToString() + " with message \"" +
exception.Message + "\"");
}
//ArgumentException Exampel
void MethodArgument(int number)
{
if (number <= 0)
throw new ArgumentException();
}
try
{
/* as -10 i not the valid argument for the function
* it will throw error
*/
MethodArgument(-10);
}
catch ( ArgumentException exception)
{
Console.WriteLine("The following error detected: " +
exception.GetType().ToString() + " with message \"" +
exception.Message + "\"");
}
//ArgumentOutOfRange Example
void methodRange(int number)
{
if (number <= 0 || number > 13)
throw new ArgumentOutOfRangeException();
}
try
{
/* As 21 is not in the range of the method defined
it will throw error
*/
methodRange(21);
}
catch(ArgumentOutOfRangeException exception)
{
Console.WriteLine("The following error detected: " +
exception.GetType().ToString() + " with message \"" +
exception.Message + "\"");
}
// SystemException Example
try
{
string myName = null;
Console.WriteLine(myName);
}
catch ( SystemException exception)
{
Console.WriteLine("The following error detected: " +
exception.GetType().ToString() + " with message \"" +
exception.Message + "\"");
}
try
{
Account account = new Account("Sergey", "P", 100);
account.Withdraw(1000);
}
catch (InvalidOperationException exception)
{
Console.WriteLine("The following error detected: "
+ exception.GetType().ToString() + " with message \"" +
exception.Message + "\"");
}
// StackOverflow example
try
{
/* Since we never exit the recursion,
* the call stack continues to grow until
* it reaches its maximum size,
*/
RecursiveMethod(0);
}
catch (StackOverflowException exception)
{
// never excutes as program terminates due to stackoverflow
Console.WriteLine("The following error detected: " +
exception.GetType().ToString() + " with message \"" +
exception.Message + "\"");
}
Console.ReadKey();
}
}
}