Skip to content

Instantly share code, notes, and snippets.

@Sedose
Last active August 2, 2025 08:40
Show Gist options
  • Select an option

  • Save Sedose/1f7d1a1e007ba55320933523ace46a53 to your computer and use it in GitHub Desktop.

Select an option

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)
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)
@Sedose
Copy link
Author

Sedose commented Aug 2, 2025

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment