1. Introduction

Exception handling is a crucial aspect of software development. When our code encounters an unexpected situation, it throws an exception. These exceptions are lifelines, providing invaluable information about what went wrong in our program. However, to effectively debug and fix issues, we need to know not only the type of exception but also the exact location where it occurred. In this tutorial, we’ll explore how to get the line number where an Exception was thrown in Kotlin.

2. Exception Handling in Kotlin

Let’s briefly revisit exception handling in Kotlin before we delve into how to obtain the line number where an exception was thrown. Kotlin, like many modern languages, uses a try-catch block to capture and handle exceptions. Let’s review the basic structure of the try-catch block in Kotlin :

try {
    // Code that might throw an exception
} catch (e: Throwable) {
    // Handle exception here
}

This try-catch block allows us to handle exceptions gracefully, but it doesn’t provide the line number where the Exception occurred by default.

2.1. The runCatching() Function

runCatching() is a higher-order function that encapsulates a block of code that might throw an exception. It returns a Result object, representing either success (the code executed without exceptions) or failure (an Exception was thrown).

Let’s see runCatching() in action:

val result: Result<Type> = runCatching {
    // Code that might throw an exception
}

result.fold(
    onSuccess = { value -> 
        // Handle the successful execution
    },
    onFailure = { exception ->
        // Handle exception here
    }
)

3. Using Stack Traces

The Exception’s stack trace includes the line number from which it was thrown. Additionally, it provides details about the call stack at the time of the exception, including method names, file names, and line numbers. The first or top entry of the stack trace is the origination point of a thrown exception. Kotlin provides a convenient way to access this information:

try {
    // Code that might throw an exception
} catch (e: Exception) {
    val stackTrace = e.stackTrace
    // do something with the stackTrace
}

Calling e.stackTrace returns detailed output that includes the line number where the exception occurred. However, to print it, we should use a dedicated Logger framework.

4. Custom Exception Handling

In some cases, we may want to extract the line number programmatically and use it for custom error handling or logging. Let’s write a function for how we can obtain the line number as an integer:

fun getLineNumber(exception: Exception): Int? {
    val stackTraceElement = exception.stackTrace.firstOrNull()
    return stackTraceElement?.lineNumber
}

In this code, we’ve defined getLineNumber(), which takes an Exception as an argument and extracts the line number from the first element of the stack trace.

Let’s take a look at this code in a catch block:

try {
    // Code that might throw an exception
} catch (e: Exception) {
    val lineNumber = getLineNumber(e)
    if (lineNumber != null) {
        println("Exception occurred at line $lineNumber")
    }
}

This custom approach allows us to incorporate the line number information into our application’s error messages, logs, or reporting mechanisms.

4.1. Advanced Custom Exception Handling

To enhance our debugging, we can add more elements to our log, such as the className from the first item of the stack trace, or which source file this class is from. In a complex application, in particular, having all the information available eases our investigation:

fun getFirstStacktraceElement(exception: Exception): String? {
    return exception.stackTrace.firstOrNull()?.className
}

fun getFileName(exception: Exception): String? {
    return exception.stackTrace.firstOrNull()?.fileName
}

Furthermore, we can adapt our code to use these functions:

try {
    // Code that might throw an exception
} catch (e: Exception) {
    val lineNumber = getLineNumber(e)
    val firstStackClassName = getFirstStacktraceElement(e)
    val fileName = getFileName(e)
    println("Exception occurred at $fileName.$firstStackClassName L#$lineNumber")  
}

5. Conclusion

Knowing the line number where an Exception was thrown allows us to debug our Kotlin applications much more effectively. With the ability to access the stack trace and extract this information, we can quickly identify the root cause of issues and implement better error-handling strategies.

Whether we choose to use a logging framework or create custom exception-handling logic, understanding the source of exceptions will streamline the debugging process and enhance the overall quality of our Kotlin code.

As always, the code used in this article is available over on GitHub.

Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.