Skip to content

SCALA-18: Exception handling #49

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.baeldung.scala.exceptionhandling

object CalculatorExceptions {
class IntOverflowException extends RuntimeException
class NegativeNumberException extends RuntimeException
}

object Calculator {
import CalculatorExceptions._

def sum(a: Int, b: Int): Int = {
if (a < 0 || b < 0) throw new NegativeNumberException
val result = a + b
if (result < 0) throw new IntOverflowException
result
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.baeldung.scala.exceptionhandling

import scala.util.{Try, Success, Failure}
import scala.util.control.Exception._

object Examples {
import CalculatorExceptions._
def tryCatch(a: Int, b: Int): Int = {
try {
return Calculator.sum(a, b)
// println(s"${a} + ${b} = ${result}")
} catch {
case e: IntOverflowException => -1
case e: NegativeNumberException => -2
} finally {
// This block will always be invoked
println("Calculation done!")
}
}

def trySuccessFailure(a: Int, b: Int): Try[Int] = Try {
Calculator.sum(a, b)
}

def catchObjects(a: Int, b: Int): Try[Int] = allCatch.withTry {
Calculator.sum(a, b)
}

val myCustomCatcher = catching(classOf[NegativeNumberException])

def customCatchObjects(a: Int, b: Int): Try[Int] = myCustomCatcher.withTry {
Calculator.sum(a, b)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
package com.baeldung.scala.exceptionhandling

import org.scalatest._
import org.scalatest.Assertions._
import scala.util.{Success, Failure}

class ExamplesUnitTest extends FlatSpec with Matchers {

"tryCatch" should "handle NegativeNumberException" in {
noException should be thrownBy Examples.tryCatch(-1, -2)
assert(Examples.tryCatch(-20, -30) == -2)
}

it should "handle IntOverflowException" in {
noException should be thrownBy Examples.tryCatch(Int.MaxValue, 1)
assert(Examples.tryCatch(Int.MaxValue, 1) == -1)
}

it should "return the correct sum" in {
noException should be thrownBy Examples.tryCatch(2, 1)
assert(Examples.tryCatch(2, 3) == 5)
}

"trySuccessFailure" should "handle NegativeNumberException" in {
import CalculatorExceptions._
val result = Examples.trySuccessFailure(-1, -2)
result match {
case Failure(e) => assert(e.isInstanceOf[NegativeNumberException])
case Success(_) => fail("Should fail!")
}
}

it should "handle IntOverflowException" in {
import CalculatorExceptions._
val result = Examples.trySuccessFailure(Int.MaxValue, 1)
result match {
case Failure(e) => assert(e.isInstanceOf[IntOverflowException])
case Success(_) => fail("Should fail!")
}
}

it should "return the correct sum" in {
import CalculatorExceptions._
val result = Examples.trySuccessFailure(3, 2)
result match {
case Failure(e) => fail("Should succed!")
case Success(result) => assert(result == 5)
}
}

"catchObjects" should "handle NegativeNumberException" in {
import CalculatorExceptions._
val result = Examples.catchObjects(-1, -2)
result match {
case Failure(e) => assert(e.isInstanceOf[NegativeNumberException])
case Success(_) => fail("Should fail!")
}
}

it should "handle IntOverflowException" in {
import CalculatorExceptions._
val result = Examples.catchObjects(Int.MaxValue, 1)
result match {
case Failure(e) => assert(e.isInstanceOf[IntOverflowException])
case Success(_) => fail("Should fail!")
}
}

it should "return the correct sum" in {
import CalculatorExceptions._
val result = Examples.catchObjects(3, 2)
result match {
case Failure(e) => fail("Should succed!")
case Success(result) => assert(result == 5)
}
}

"customCatchObjects" should "handle NegativeNumberException" in {
import CalculatorExceptions._
val result = Examples.customCatchObjects(-1, -2)
result match {
case Failure(e) => assert(e.isInstanceOf[NegativeNumberException])
case Success(_) => fail("Should fail!")
}
}

it should "handle IntOverflowException" in {
import CalculatorExceptions._
assertThrows[IntOverflowException] {
Examples.customCatchObjects(Int.MaxValue, 1)
}
}

it should "return the correct sum" in {
import CalculatorExceptions._
val result = Examples.customCatchObjects(3, 2)
result match {
case Failure(e) => fail("Should succed!")
case Success(result) => assert(result == 5)
}
}

"customCatchObjects composed with trySuccessFailure" should "return the correct sum" in {
import CalculatorExceptions._
val result = Examples.customCatchObjects(3, 2) flatMap (Examples
.trySuccessFailure(_, 3))
result match {
case Failure(e) => fail("Should succed!")
case Success(result) => assert(result == 8)
}
}

it should "print an error" in {
import CalculatorExceptions._
val result = Examples.customCatchObjects(-1, -2) flatMap (Examples
.trySuccessFailure(_, 3))
result match {
case Failure(e) => assert(e.isInstanceOf[NegativeNumberException])
case Success(result) => fail("Should fail!")
}
}
}