-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cs
80 lines (64 loc) · 1.87 KB
/
main.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
// Courey Elliott
// try/catch example
using System;
public class MainMethod
{
public static void Main (string[] args)
{
try
{
try
{
Console.Write ("Enter an equation:");
string input = Console.ReadLine ().Trim ();
string[] terms = input.Split (new Char[] { ' ', '+', '-', '*', '/' }, StringSplitOptions.RemoveEmptyEntries);
int firstNumber = Convert.ToInt32 (terms [0]);
if (input [0] == '-')
{
firstNumber = -firstNumber;
input = input.Remove (0, 1);
}
input = input.Remove (0, terms [0].Length).Trim ();
char op = input [0];
input = input.Remove (0, 1).Trim ();
int secondNumber = Convert.ToInt32 (terms [1]);
if (input [0] == '-')
secondNumber = -secondNumber;
int result = 0;
if (op == '+')
result = (firstNumber + secondNumber);
else if (op == '-')
result = (firstNumber - secondNumber);
else if (op == '*')
result = (firstNumber * secondNumber);
else if (op == '/')
result = (firstNumber / secondNumber);
Console.WriteLine (result);
}// end inner try
catch (FormatException e)
{
Console.WriteLine ("Invalid input. Please input an integer.");
}
catch (ArgumentNullException e)
{
Console.WriteLine ("You gotta put numbers in if you want an answer.");
}
catch (ArgumentException e)
{
Console.WriteLine("Invalid argument. Formula accepts only 2 integers.");
}
catch (OverflowException)
{
Console.WriteLine("Number must be between -2147483647 and 2147483646.");
}
catch (Exception e) //catches anything that isn't caught before and throws it to the outside try/catch
{
throw;
}
}// end outer try
catch (Exception e) //catches exception e thrown from inner catch
{
Console.WriteLine("General exception thrown from inner catch's catch all exception handling. Could do something like save here etc...");
}
}
}