-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathErrorhandling_part1.swift
108 lines (97 loc) · 2.02 KB
/
Errorhandling_part1.swift
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
enum EligibilityError: Int, Error
{
case NotEligibleForCm = 18
case NotEligibleForPresident = 25
case NotEligibleForVoting = 35
}
func eligibility(age: Int) throws -> Void{
if age < EligibilityError.NotEligibleForVoting.rawValue
{
throw EligibilityError.NotEligibleForVoting
}
else if age < EligibilityError.NotEligibleForCm.rawValue
{
print("eligible for voting")
throw EligibilityError.NotEligibleForCm
}
else if age < EligibilityError.NotEligibleForPresident.rawValue
{
print("eligible for voting and cm")
throw EligibilityError.NotEligibleForPresident
}
else{
print("eligible for voting, cm and president")
}
}
var age = 34
do
{
try eligibility(age: age)
}
catch EligibilityError.NotEligibleForVoting{
print("not eligible for voting, cm and president")
}
catch EligibilityError.NotEligibleForCm
{
print("not eligible for cm, president")
}
catch EligibilityError.NotEligibleForPresident
{
print("not eligible for president")
}
//Nested do - catch
enum NumberError: Error
{
case WholeNUM
case NaturalNUM
}
func wholeNum(num: Int) throws -> Void
{
if num < 0
{
throw NumberError.WholeNUM
}
}
func naturalNum(num: Int) throws -> Void
{
if num <= 0
{
throw NumberError.NaturalNUM
}
}
var num = -1
do{
try wholeNum(num: num)
print("It is whole Num")
do
{
try naturalNum(num: num)
print("It is natural num")
}
catch NumberError.NaturalNUM{
print("It is not natural Num")
}
}
catch NumberError.WholeNUM{
print("It is not whole Number")
}
catch{
print(error)
}
// runtime error
enum DivisionError: Error {
case DivisionByZero
}
func divide(num1: Int, num2: Int) throws -> Int {
if num2 == 0 {
throw DivisionError.DivisionByZero
}
return num1 / num2
}
do
{
try divide(num1: 10, num2: 0)
}
catch {
print(error)// here i need error msg division by zero what the system tells if dont use try catch
}