Added exception handling examples

This commit is contained in:
Anirban Chatterjee 2020-07-13 02:19:13 +02:00
parent 44c664a34d
commit 793db9f42b
2 changed files with 129 additions and 0 deletions

View File

@ -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")
}
}

View File

@ -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<IllegalArgumentException> { classUnderTest.throwKeyword() }
}
@Test
fun whenAnnotationUsed_thenThrowsException(){
assertThrows<IOException> { classUnderTest.throwsAnnotation() }
}
}