0
0
Kotlinprogramming~10 mins

Custom exception classes in Kotlin - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Custom exception classes
Define custom exception class
Throw custom exception
Catch exception in try-catch
Handle or print exception message
End
This flow shows how to create a custom exception, throw it, catch it, and handle it in Kotlin.
Execution Sample
Kotlin
class MyException(message: String) : Exception(message)

fun test() {
  throw MyException("Oops!")
}

fun main() {
  try { test() } catch (e: MyException) { println(e.message) }
}
This code defines a custom exception, throws it in a function, and catches it in main to print the message.
Execution Table
StepActionEvaluationResult
1Define class MyExceptionclass MyException(message: String) : Exception(message)Class created
2Call test()test() calledInside test function
3Throw MyException("Oops!")throw MyException("Oops!")Exception thrown with message 'Oops!'
4Catch exception in main try-catchcatch (e: MyException)Exception caught
5Print e.messageprintln(e.message)Output: Oops!
💡 Exception caught and handled, program ends normally.
Variable Tracker
VariableStartAfter throwAfter catchFinal
e (exception)nullMyException("Oops!") thrownMyException("Oops!") caughtMyException("Oops!")
Key Moments - 3 Insights
Why do we create a class that extends Exception?
Creating a class that extends Exception lets us make a special error type to identify and handle specific problems, as shown in step 1 and step 3 of the execution_table.
What happens when we throw the custom exception?
Throwing the exception immediately stops normal flow and jumps to the nearest catch block that matches, as seen in step 3 and step 4.
Why do we catch the exception in main?
Catching the exception prevents the program from crashing and lets us handle the error gracefully, shown in step 4 and step 5.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is printed at step 5?
A"Error occurred"
BNothing is printed
C"Oops!"
DProgram crashes
💡 Hint
Check the 'Result' column at step 5 in execution_table.
At which step is the custom exception created?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Look at the 'Action' and 'Result' columns in execution_table for class creation.
If we remove the catch block, what happens?
AException is ignored
BProgram crashes with uncaught exception
CProgram prints "Oops!" automatically
DNothing changes
💡 Hint
Refer to key_moments about catching exceptions to prevent crashes.
Concept Snapshot
Custom exception classes in Kotlin:
- Define by extending Exception class
- Throw using throw keyword
- Catch with try-catch block
- Allows specific error handling
- Prevents program crashes by handling errors
Full Transcript
This example shows how to create a custom exception class in Kotlin by extending the Exception class. The custom exception is thrown inside a function called test(). In the main function, the test() call is wrapped in a try-catch block that catches the custom exception type. When the exception is thrown, normal execution stops and control jumps to the catch block. The catch block prints the exception's message, "Oops!", preventing the program from crashing. This flow helps handle specific errors clearly and safely.