Last active
August 2, 2025 08:40
-
-
Save Sedose/1f7d1a1e007ba55320933523ace46a53 to your computer and use it in GitHub Desktop.
Java Exceptions preserve stacktrace even without throwing (even when we just return Exception from mehtod)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Main { | |
| public static void main(String[] args) { | |
| Exception exception = new Main().invokeTopLevel(); | |
| exception.printStackTrace(); | |
| } | |
| Exception invokeTopLevel() { | |
| return invokeSecondLevel(); | |
| } | |
| private Exception invokeSecondLevel() { | |
| return invokeThirdLevel(); | |
| } | |
| private Exception invokeThirdLevel() { | |
| return invokeFourthLevel(); | |
| } | |
| private Exception invokeFourthLevel() { | |
| return invokeFifthLevel(); | |
| } | |
| private Exception invokeFifthLevel() { | |
| return new IllegalStateException(); | |
| } | |
| } | |
| // This might be useful for integration Kotlin 2.4 Rich Errors with exceptions. | |
| // Then in Kotlin 2.4 we wrap that exception like that: return Error(causeException). | |
| // Typedef: | |
| // error class ParsingError(val cause: Exception) |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Rich Errors presentation: https://resources.jetbrains.com/storage/products/kotlinconf-2025/may-22/Rich%20Errors%20in%20Kotlin%20_%20Michail%20Zarec%CC%8Censkij.pdf