From 84fd0a92e61d413f6f9f44d0d626aa4c8b80432d Mon Sep 17 00:00:00 2001 From: Anirban Chatterjee Date: Mon, 13 Jul 2020 02:19:13 +0200 Subject: [PATCH 1/3] 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() } + } +} From 64202c94d63ce7ce406f6111a10269ccf6251944 Mon Sep 17 00:00:00 2001 From: Anirban Chatterjee Date: Thu, 16 Jul 2020 00:38:26 +0200 Subject: [PATCH 2/3] Minor code refinement --- .../com/baeldung/exceptionhandling/ExceptionHandling.kt | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) 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 index 137a303edb..225a1339e2 100644 --- 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 @@ -28,7 +28,7 @@ class ExceptionHandling { fun multipleCatchBlock(): Int? { return try { val result = 25 / 0 - return result + result } catch (exception: NumberFormatException) { println("NumberFormatException in the code") null @@ -69,10 +69,10 @@ class ExceptionHandling { } } - fun throwKeyword() { + fun throwKeyword(): Int { val message = "Welcome to Kotlin Tutorials" if (message.length > 10) throw IllegalArgumentException("String is invalid") - else println("String is valid") + else return message.length } @Throws(IOException::class) From 17816e146bea183ddf6900a2b9169e558a6e94d5 Mon Sep 17 00:00:00 2001 From: Anirban Chatterjee Date: Sat, 18 Jul 2020 12:54:29 +0200 Subject: [PATCH 3/3] Moved to new module --- .../com/baeldung/exceptionhandling/ExceptionHandling.kt | 5 +++++ .../baeldung/exceptionhandling/ExceptionHandlingUnitTest.kt | 5 +++++ 2 files changed, 10 insertions(+) rename core-kotlin-modules/{core-kotlin-lang-2 => core-kotlin}/src/main/kotlin/com/baeldung/exceptionhandling/ExceptionHandling.kt (93%) rename core-kotlin-modules/{core-kotlin-lang-2 => core-kotlin}/src/test/kotlin/com/baeldung/exceptionhandling/ExceptionHandlingUnitTest.kt (88%) diff --git a/core-kotlin-modules/core-kotlin-lang-2/src/main/kotlin/com/baeldung/exceptionhandling/ExceptionHandling.kt b/core-kotlin-modules/core-kotlin/src/main/kotlin/com/baeldung/exceptionhandling/ExceptionHandling.kt similarity index 93% rename from core-kotlin-modules/core-kotlin-lang-2/src/main/kotlin/com/baeldung/exceptionhandling/ExceptionHandling.kt rename to core-kotlin-modules/core-kotlin/src/main/kotlin/com/baeldung/exceptionhandling/ExceptionHandling.kt index 225a1339e2..6689ab43e4 100644 --- a/core-kotlin-modules/core-kotlin-lang-2/src/main/kotlin/com/baeldung/exceptionhandling/ExceptionHandling.kt +++ b/core-kotlin-modules/core-kotlin/src/main/kotlin/com/baeldung/exceptionhandling/ExceptionHandling.kt @@ -75,6 +75,11 @@ class ExceptionHandling { else return message.length } + fun throwExpression(): Int? { + val message: String? = null + return message?.length ?: throw IllegalArgumentException("String is null") + } + @Throws(IOException::class) fun throwsAnnotation(): String?{ val filePath = null diff --git a/core-kotlin-modules/core-kotlin-lang-2/src/test/kotlin/com/baeldung/exceptionhandling/ExceptionHandlingUnitTest.kt b/core-kotlin-modules/core-kotlin/src/test/kotlin/com/baeldung/exceptionhandling/ExceptionHandlingUnitTest.kt similarity index 88% rename from core-kotlin-modules/core-kotlin-lang-2/src/test/kotlin/com/baeldung/exceptionhandling/ExceptionHandlingUnitTest.kt rename to core-kotlin-modules/core-kotlin/src/test/kotlin/com/baeldung/exceptionhandling/ExceptionHandlingUnitTest.kt index 49ad3c38e0..af7aa4406f 100644 --- a/core-kotlin-modules/core-kotlin-lang-2/src/test/kotlin/com/baeldung/exceptionhandling/ExceptionHandlingUnitTest.kt +++ b/core-kotlin-modules/core-kotlin/src/test/kotlin/com/baeldung/exceptionhandling/ExceptionHandlingUnitTest.kt @@ -39,6 +39,11 @@ class ExceptionHandlingUnitTest { assertThrows { classUnderTest.throwKeyword() } } + @Test + fun givenIllegalArgument_whenElvisExpressionUsed_thenThrowsException(){ + assertThrows { classUnderTest.throwExpression() } + } + @Test fun whenAnnotationUsed_thenThrowsException(){ assertThrows { classUnderTest.throwsAnnotation() }