Exception handling in Kotlin doesn't seem effective

Apologies if this question is super-obvious. I’m not from a Java background so a lot of this is foreign to me.

I have a project I’m working on that uses the “org.jetbrains.exposed.sql” module to interact with an sqlite database. Everything about the module works fine, except… the project dies if the database isn’t found. This should be a possible option, since the database is held on a network drive, and the connectivity may be poor. I’m tearing my hair out trying to figure out how to handle that scenario in Kotlin. I’ve googled every youtube video and tutorial on exception handling and I’m 1000% clear on how to handle an exception in the abstract, but this simply NEVER works when running the command to connect to the database. (it absolutely DOES work when attempting other processes that could fail, like the often-tutorialed divide-by-zero)

For example:

try {
Database.connect("jdbc:sqlite:$ databaseName ", “org.sqlite.JDBC”)

catch (e:SQLException){
return false
return true

This should return false if there was an exception and true if the database connected, however if the database is not found, the program completely crashes rather than handling and returning false.

[main] WARN Exposed - Transaction attempt #0 failed: path to ‘badpath’: ‘\’ does not exist. Statement(s): null

java.sql.SQLException: path to ‘badpath’: ‘\’ does not exist

at org.sqlite.SQLiteConnection.open(SQLiteConnection.java:215)

Process finished with exit code 1

As you can see, the exception IS an SQLException (although it makes my brain hurt that you have to guess all of the exact types of exceptions your code might ever theoretically have for the try-catch to be able to handle it)

So… What am I doing wrong here? Is there a more robust way to tell Kotlin to try something and just let me know if it failed instead of committing ritual suicide?

Thanks for the reply. To my eye, that looks like exactly what I posted as my current (non-working) code.

That is as far as I know the correct way to handle an exception in Kotlin, (and it works in other cases). Makes me wonder if this is a bug in the module instead.

The code you post should work, assuming the library fails with an SQLException. Make sure you catch the right exception type. Check your imports that you import SQLException from the right package and don’t accidentally use a different one than the library.
Other than that I can’t think of anything.

1 Like

I also tried “Throwable” in an attempt to have it execute the catch block on ANY sort of exception. (which failed in the same way)
Is there a term I can use that’s even more generic than “Throwable”?

I mean… I understand the concept of occasionally wanting different catch blocks for different exceptions, but it seems like there should be a way to catch ALL exceptions without always knowing in advance which exception out of the millions possible your code might encounter.

This is super strange. I never had problems with try catch, both catching Throwable or any specific exception. Can you share your project (github) or maybe just the file as a gist? Maybe there is something else wrong which leads to this strange behavior (although I can’t think of what might cause this).

Are you really sure that the exception is thrown at this point? Details are missing at the stacktrace.


OK, thanks everyone for your help. While trying to generate a snippet that reliably generates the error, I realized that the next step (a transaction) was the one actually throwing the exception. I had hoped that attempting to connect to a database that didn’t exist would generate an error and I could use that branch to skip the transaction section and move on to an error screen. It turns out that exposed is perfectly OK with connecting to nonexistent databases, but tosses the exception when attempting transactions on them.

Placing my try catch block around the transaction instead has resolved the issue.

Thanks again everyone for your help.