• Checked exceptions
  • Unchecked exceptions
  • <">

    How to throw custom exception in Kotlin?



    Exception is an important aspect of any programming language. It prevents our code from generating incorrect output at runtime. There are two types of exceptions −

    • Checked exceptions
    • Unchecked exceptions

    Checked Exceptions

    Checked exceptions are those which are checked at the compile time. As per example, FileNotFoundException() or IOException. In the following example, we will see how we can generate a checked Exception.

    Example

    import java.io.File
    import java.io.InputStream
    
    fun main(args: Array<String>) {
       try {
          val inputStream: InputStream = File("Hello.txt").inputStream()
       } catch(e:Exception) {
          e.printStackTrace();
       }
    }

    Output

    Once you execute this code, it will generate the following output in the result section. You could see that while compiling, we have got the checked exception as an output.

    java.io.FileNotFoundException: Hello.txt (No such file or directory)
       at java.io.FileInputStream.open0(Native Method)
       at java.io.FileInputStream.open(FileInputStream.java:195)
       at java.io.FileInputStream.<init>(FileInputStream.java:138)
       at MainKt.main(main.kt:6)

    Unchecked Exceptions

    Unchecked exceptions are those which are not checked at the compile time; rather they will be thrown at the runtime. As per example, we can consider any of the ArithmeticException, NumberFormatException. In the following example, we will see how we can generate an unchecked Exception.

    Example

    fun main(args: Array<String>) {
       try {
          val myVar:Int = 12;
          val v:String = "Tutorialspoint.com";
          v.toInt();
       } catch(e:Exception) {
          e.printStackTrace();
       } finally {
           println("Exception Handeling in Kotlin");
       }
    }

    Output

    It will produce the following output −

    Exception Handeling in Kotlin
    java.lang.NumberFormatException: For input string: "Tutorialspoint.com"
       at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
       at java.lang.Integer.parseInt(Integer.java:580)
       at java.lang.Integer.parseInt(Integer.java:615)
       at MainKt.main(main.kt:5)

    Custom Exception in Kotlin

    The concept of exception in Kotlin is very much same as it is in Java. All the exceptions in Kotlin are the descendants of the Throwable class. In Kotlin, developers do have the privilege to create their own custom Exception. Custom Exceptions are a part of unchecked exception which means they will be thrown at the runtime.

    Example of Custom Exception

    We will create our own custom exception using a very simple example. In this example, we are declaring a variable and checking whether that variable value is less than 50. Depending on the outcome, we will throw a custom exception using Kotlin built-in functionality.

    fun main(args: Array<String>) {
       val sampleNumber:Int;
       sampleNumber = 100;
    
       if(sampleNumber > 50) {
          // throwing custom exception instead of checked Exception
          throw myCustomException("Invalid Input. Enter a correct number")
       } else {
          println("Welcome!! You have entered a correct value")
       }
    }
    
    // declaring custom exception class
    class myCustomException(message: String) : Exception(message)

    Output

    When we execute this code, it will yield the following output. You can observe that we are throwing our custom exception along with the passed message.

    Exception in thread "main" myCustomException: Invalid Input. Enter a correct number
       at MainKt.main(main.kt:7)
    Kickstart Your Career

    Get certified by completing the course

    Get Started
    Advertisements