Skip to content

Commit 364eba2

Browse files
authored
Merge pull request #49 from barbasa/master
SCALA-18: Exception handling
2 parents 560b11d + 630ca33 commit 364eba2

File tree

3 files changed

+173
-0
lines changed

3 files changed

+173
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.baeldung.scala.exceptionhandling
2+
3+
object CalculatorExceptions {
4+
class IntOverflowException extends RuntimeException
5+
class NegativeNumberException extends RuntimeException
6+
}
7+
8+
object Calculator {
9+
import CalculatorExceptions._
10+
11+
def sum(a: Int, b: Int): Int = {
12+
if (a < 0 || b < 0) throw new NegativeNumberException
13+
val result = a + b
14+
if (result < 0) throw new IntOverflowException
15+
result
16+
}
17+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.baeldung.scala.exceptionhandling
2+
3+
import scala.util.{Try, Success, Failure}
4+
import scala.util.control.Exception._
5+
6+
object Examples {
7+
import CalculatorExceptions._
8+
def tryCatch(a: Int, b: Int): Int = {
9+
try {
10+
return Calculator.sum(a, b)
11+
// println(s"${a} + ${b} = ${result}")
12+
} catch {
13+
case e: IntOverflowException => -1
14+
case e: NegativeNumberException => -2
15+
} finally {
16+
// This block will always be invoked
17+
println("Calculation done!")
18+
}
19+
}
20+
21+
def trySuccessFailure(a: Int, b: Int): Try[Int] = Try {
22+
Calculator.sum(a, b)
23+
}
24+
25+
def catchObjects(a: Int, b: Int): Try[Int] = allCatch.withTry {
26+
Calculator.sum(a, b)
27+
}
28+
29+
val myCustomCatcher = catching(classOf[NegativeNumberException])
30+
31+
def customCatchObjects(a: Int, b: Int): Try[Int] = myCustomCatcher.withTry {
32+
Calculator.sum(a, b)
33+
}
34+
}
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
package com.baeldung.scala.exceptionhandling
2+
3+
import org.scalatest._
4+
import org.scalatest.Assertions._
5+
import scala.util.{Success, Failure}
6+
7+
class ExamplesUnitTest extends FlatSpec with Matchers {
8+
9+
"tryCatch" should "handle NegativeNumberException" in {
10+
noException should be thrownBy Examples.tryCatch(-1, -2)
11+
assert(Examples.tryCatch(-20, -30) == -2)
12+
}
13+
14+
it should "handle IntOverflowException" in {
15+
noException should be thrownBy Examples.tryCatch(Int.MaxValue, 1)
16+
assert(Examples.tryCatch(Int.MaxValue, 1) == -1)
17+
}
18+
19+
it should "return the correct sum" in {
20+
noException should be thrownBy Examples.tryCatch(2, 1)
21+
assert(Examples.tryCatch(2, 3) == 5)
22+
}
23+
24+
"trySuccessFailure" should "handle NegativeNumberException" in {
25+
import CalculatorExceptions._
26+
val result = Examples.trySuccessFailure(-1, -2)
27+
result match {
28+
case Failure(e) => assert(e.isInstanceOf[NegativeNumberException])
29+
case Success(_) => fail("Should fail!")
30+
}
31+
}
32+
33+
it should "handle IntOverflowException" in {
34+
import CalculatorExceptions._
35+
val result = Examples.trySuccessFailure(Int.MaxValue, 1)
36+
result match {
37+
case Failure(e) => assert(e.isInstanceOf[IntOverflowException])
38+
case Success(_) => fail("Should fail!")
39+
}
40+
}
41+
42+
it should "return the correct sum" in {
43+
import CalculatorExceptions._
44+
val result = Examples.trySuccessFailure(3, 2)
45+
result match {
46+
case Failure(e) => fail("Should succed!")
47+
case Success(result) => assert(result == 5)
48+
}
49+
}
50+
51+
"catchObjects" should "handle NegativeNumberException" in {
52+
import CalculatorExceptions._
53+
val result = Examples.catchObjects(-1, -2)
54+
result match {
55+
case Failure(e) => assert(e.isInstanceOf[NegativeNumberException])
56+
case Success(_) => fail("Should fail!")
57+
}
58+
}
59+
60+
it should "handle IntOverflowException" in {
61+
import CalculatorExceptions._
62+
val result = Examples.catchObjects(Int.MaxValue, 1)
63+
result match {
64+
case Failure(e) => assert(e.isInstanceOf[IntOverflowException])
65+
case Success(_) => fail("Should fail!")
66+
}
67+
}
68+
69+
it should "return the correct sum" in {
70+
import CalculatorExceptions._
71+
val result = Examples.catchObjects(3, 2)
72+
result match {
73+
case Failure(e) => fail("Should succed!")
74+
case Success(result) => assert(result == 5)
75+
}
76+
}
77+
78+
"customCatchObjects" should "handle NegativeNumberException" in {
79+
import CalculatorExceptions._
80+
val result = Examples.customCatchObjects(-1, -2)
81+
result match {
82+
case Failure(e) => assert(e.isInstanceOf[NegativeNumberException])
83+
case Success(_) => fail("Should fail!")
84+
}
85+
}
86+
87+
it should "handle IntOverflowException" in {
88+
import CalculatorExceptions._
89+
assertThrows[IntOverflowException] {
90+
Examples.customCatchObjects(Int.MaxValue, 1)
91+
}
92+
}
93+
94+
it should "return the correct sum" in {
95+
import CalculatorExceptions._
96+
val result = Examples.customCatchObjects(3, 2)
97+
result match {
98+
case Failure(e) => fail("Should succed!")
99+
case Success(result) => assert(result == 5)
100+
}
101+
}
102+
103+
"customCatchObjects composed with trySuccessFailure" should "return the correct sum" in {
104+
import CalculatorExceptions._
105+
val result = Examples.customCatchObjects(3, 2) flatMap (Examples
106+
.trySuccessFailure(_, 3))
107+
result match {
108+
case Failure(e) => fail("Should succed!")
109+
case Success(result) => assert(result == 8)
110+
}
111+
}
112+
113+
it should "print an error" in {
114+
import CalculatorExceptions._
115+
val result = Examples.customCatchObjects(-1, -2) flatMap (Examples
116+
.trySuccessFailure(_, 3))
117+
result match {
118+
case Failure(e) => assert(e.isInstanceOf[NegativeNumberException])
119+
case Success(result) => fail("Should fail!")
120+
}
121+
}
122+
}

0 commit comments

Comments
 (0)