From 793db9f42b2e8cb3fde61072ee8f9a99d633745e Mon Sep 17 00:00:00 2001 From: Anirban Chatterjee Date: Mon, 13 Jul 2020 02:19:13 +0200 Subject: [PATCH] Added exception handling examples --- .../exceptionhandling/ExceptionHandling.kt | 83 +++++++++++++++++++ .../ExceptionHandlingUnitTest.kt | 46 ++++++++++ 2 files changed, 129 insertions(+) create mode 100644 core-kotlin-modules/core-kotlin-lang-2/src/main/kotlin/com/baeldung/exceptionhandling/ExceptionHandling.kt create mode 100644 core-kotlin-modules/core-kotlin-lang-2/src/test/kotlin/com/baeldung/exceptionhandling/ExceptionHandlingUnitTest.kt diff --git a/core-kotlin-modules/core-kotlin-lang-2/src/main/kotlin/com/baeldung/exceptionhandling/ExceptionHandling.kt b/core-kotlin-modules/core-kotlin-lang-2/src/main/kotlin/com/baeldung/exceptionhandling/ExceptionHandling.kt new file mode 100644 index 0000000000..137a303edb --- /dev/null +++ b/core-kotlin-modules/core-kotlin-lang-2/src/main/kotlin/com/baeldung/exceptionhandling/ExceptionHandling.kt @@ -0,0 +1,83 @@ +package com.baeldung.exceptionhandling + +import java.io.IOException + +class ExceptionHandling { + + fun tryCatchBlock(): Int? { + try { + val message = "Welcome to Kotlin Tutorials" + return message.toInt() + } catch (exception: NumberFormatException) { + println("NumberFormatException in the code") + return null + } + } + + fun tryCatchExpression(): Int? { + val number = try { + val message = "Welcome to Kotlin Tutorials" + message.toInt() + } catch (exception: NumberFormatException) { + println("NumberFormatException in the code") + null + } + return number + } + + fun multipleCatchBlock(): Int? { + return try { + val result = 25 / 0 + return result + } catch (exception: NumberFormatException) { + println("NumberFormatException in the code") + null + } catch (exception: ArithmeticException) { + println("ArithmeticException in the code") + null + } catch (exception: Exception) { + println("Exception in the code") + null + } + } + + fun nestedTryCatchBlock(): Int? { + return try { + val firstNumber = 50 / 2 * 0 + try { + val secondNumber = 100 / firstNumber + secondNumber + } catch (exception: ArithmeticException) { + println("ArithmeticException in the code") + null + } + } catch (exception: NumberFormatException) { + println("NumberFormatException in the code") + null + } + } + + fun finallyBlock(): Int? { + return try { + val message = "Welcome to Kotlin Tutorials" + message.toInt() + } catch (exception: NumberFormatException) { + println("NumberFormatException in the code") + null + } finally { + println("In the Finally block") + } + } + + fun throwKeyword() { + val message = "Welcome to Kotlin Tutorials" + if (message.length > 10) throw IllegalArgumentException("String is invalid") + else println("String is valid") + } + + @Throws(IOException::class) + fun throwsAnnotation(): String?{ + val filePath = null + return filePath ?: throw IOException("File path is invalid") + } +} diff --git a/core-kotlin-modules/core-kotlin-lang-2/src/test/kotlin/com/baeldung/exceptionhandling/ExceptionHandlingUnitTest.kt b/core-kotlin-modules/core-kotlin-lang-2/src/test/kotlin/com/baeldung/exceptionhandling/ExceptionHandlingUnitTest.kt new file mode 100644 index 0000000000..49ad3c38e0 --- /dev/null +++ b/core-kotlin-modules/core-kotlin-lang-2/src/test/kotlin/com/baeldung/exceptionhandling/ExceptionHandlingUnitTest.kt @@ -0,0 +1,46 @@ +package com.baeldung.exceptionhandling + +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.assertThrows +import java.io.IOException +import kotlin.test.assertNull + +class ExceptionHandlingUnitTest { + + private val classUnderTest: ExceptionHandling = ExceptionHandling() + + @Test + fun givenInvalidConversion_whenTryCatchUsed_thenReturnsCatchBlockValue(){ + assertNull(classUnderTest.tryCatchBlock()) + } + + @Test + fun givenInvalidConversion_whenTryCatchExpressionUsed_thenReturnsCatchBlockValue(){ + assertNull(classUnderTest.tryCatchExpression()) + } + + @Test + fun givenDivisionByZero_whenMultipleCatchUsed_thenReturnsCatchBlockValue(){ + assertNull(classUnderTest.multipleCatchBlock()) + } + + @Test + fun givenDivisionByZero_whenNestedTryCatchUsed_thenReturnsNestedCatchBlockValue(){ + assertNull(classUnderTest.nestedTryCatchBlock()) + } + + @Test + fun givenInvalidConversion_whenTryCatchFinallyUsed_thenReturnsCatchAndFinallyBlock(){ + assertNull(classUnderTest.finallyBlock()) + } + + @Test + fun givenIllegalArgument_whenThrowKeywordUsed_thenThrowsException(){ + assertThrows { classUnderTest.throwKeyword() } + } + + @Test + fun whenAnnotationUsed_thenThrowsException(){ + assertThrows { classUnderTest.throwsAnnotation() } + } +}