Merge pull request #9690 from anirban99/feature/ktln-29/exception-handling-in-kotlin
KTLN-29 : Exception handling in Kotlin
This commit is contained in:
commit
0d6e31f969
|
@ -0,0 +1,88 @@
|
||||||
|
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
|
||||||
|
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(): Int {
|
||||||
|
val message = "Welcome to Kotlin Tutorials"
|
||||||
|
if (message.length > 10) throw IllegalArgumentException("String is invalid")
|
||||||
|
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
|
||||||
|
return filePath ?: throw IOException("File path is invalid")
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,51 @@
|
||||||
|
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 givenIllegalArgument_whenElvisExpressionUsed_thenThrowsException(){
|
||||||
|
assertThrows<IllegalArgumentException> { classUnderTest.throwExpression() }
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun whenAnnotationUsed_thenThrowsException(){
|
||||||
|
assertThrows<IOException> { classUnderTest.throwsAnnotation() }
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue