Skip to content

Commit 6282c3d

Browse files
committed
finish improvement control exceptions
1 parent 9febc29 commit 6282c3d

File tree

4 files changed

+60
-16
lines changed

4 files changed

+60
-16
lines changed

src/main/worksheets/e_Exception/d_controlException.sc

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,14 @@ import scala.util.control.Exception._
1616
* composing exception handlers rather than composing behavior.
1717
*/
1818
// allCatch
19+
// TODO -me- use allCatch and nonFatalCatch with opt
20+
// use "42".toInt and "abc".toInt
1921
val opt1:Option[Int] = allCatch.opt("42".toInt)
2022
val opt2:Option[Int] = nonFatalCatch.opt("abc".toInt)
2123

24+
25+
// TODO -me- use allCatch with withTry
26+
// use "42".toInt and "abc".toInt
2227
val try1:Try[Int] = allCatch.withTry("abc".toInt)
2328
val try2:Try[Int] = allCatch.withTry("42".toInt)
2429
//
@@ -27,7 +32,10 @@ val try2:Try[Int] = allCatch.withTry("42".toInt)
2732
// -2- Generator
2833
// ====================================
2934
//
30-
// TODO -me- lt's write an handler which catch the
35+
// TODO -me- lt's write an handler which catch the
36+
// - NumberFormatException -> return 0
37+
// and
38+
// - IllegalArgumentException -> return -1
3139
/**
3240
* container which catch the defined exception
3341
*/
@@ -53,18 +61,36 @@ assert(test1 == 42)
5361
val test2: Int = handleIntConverting {"abc42def".toInt}
5462
assert(test2 == 0)
5563

56-
64+
//
5765
// TODO -2- use handleIntConverting and throw an IllegalArgumentException
58-
// TODO write the assert statement before u implement
66+
// write the assert statement before you implement
5967
val test3: Int = handleIntConverting {throw new IllegalArgumentException}
6068
assert(test3 == -1)
6169

62-
63-
// TODO -3- use handleIntConverting and throw an IndexOutOfBoundsException
64-
// TODO write the assert statement before u implement
70+
//
71+
// TODO -3- what should happend if you throw an
72+
// IndexOutOfBoundsException in withApply?
6573
val test4: Int = handleIntConverting {
6674
"42".toInt
67-
throw new IndexOutOfBoundsException
75+
// throw new IndexOutOfBoundsException
6876
}
6977

78+
//
79+
//
80+
// ====================================
81+
// -3- Handling
82+
// ====================================
83+
//
84+
// TODO -me- short analogy to try/catch
85+
val test5: Int = handling[Int](classOf[NumberFormatException])
86+
.by (_ =>1) apply "sd".toInt
87+
88+
// TODO -1- can u identify the analogy?
89+
val test6: Int = try {
90+
"sb".toInt
91+
}
92+
catch {
93+
case _: NumberFormatException => 1
94+
}
7095

96+
assert(test5 == test6)

src/main/worksheets/e_Exception/d_controlException_1_intro.sc

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@ import scala.util.Try
22
import scala.util.control.Exception._
33

44

5-
// TODO -me- use allCatch and nonFatalCatch with opt
5+
// TODO -me- use allCatch and nonFatalCatch with opt
6+
// use "42".toInt and "abc".toInt
67
val opt1:Option[Int] = ???
78
val opt2:Option[Int] = ???
89

910

10-
// TODO -me- use allCatch with withTry
11+
// TODO -me- use allCatch with withTry
12+
// use "42".toInt and "abc".toInt
1113
val try1:Try[Int] = ???
1214
val try2:Try[Int] = ???
Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import scala.util.control.Exception._
22

33

4-
// TODO -me- lt's write an handler which catch the
5-
// - NumberFormatException -> return 0
6-
// and
7-
// - IllegalArgumentException -> return -1
4+
// TODO -me- lt's write an handler which catch the
5+
// - NumberFormatException -> return 0
6+
// and
7+
// - IllegalArgumentException -> return -1
88
val handleIntConverting:Catch[Int] = ???
99

1010

@@ -19,9 +19,11 @@ assert(test2 == 0)
1919

2020

2121
// TODO -2- use handleIntConverting and throw an IllegalArgumentException
22-
// write the assert statement before u implement
22+
// write the assert statement before you implement
2323

2424

25-
// TODO -3- use handleIntConverting and throw an IndexOutOfBoundsException
26-
// what should be happend?
25+
// TODO -3- what should happend if you throw an
26+
// IndexOutOfBoundsException in withApply?
2727

28+
29+
// TODO -me- short analogy to try/catch
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import scala.util.control.Exception._
2+
3+
// TODO -me- write handler
4+
val test5: Int = ???
5+
6+
// TODO -1- can u identify the analogy?
7+
val test6: Int = try {
8+
"sb".toInt
9+
}
10+
catch {
11+
case _: NumberFormatException => 1
12+
}
13+
14+
assert(test5 == test6)

0 commit comments

Comments
 (0)