I wrote a safeMethod() which should not throw any exceptions, and if it does, I want to crash the app (to get them reported via crash monitoring system).
The problem is, I need to call this method from library callback which is wrapped in try/catch up the call stack.
I tried to "raise stakes":
try {
safeMethod();
} catch (Exception e) {
throw new Throwable(e);
}
But this does not compile because overridden method does not declare throws Throwable.
The other thing that comes to my mind is to use some asynchrony, for example:
new Handler().post(() -> safeMethod()); // Handler is Android class to work with thread's message queue
or
new Thread(() -> safeMethod()).start();
It works, but seems like an overkill, complicating control flow and wasting resources.
Are there any better ways?
I searched here and on the web for "prevent catching exception", "override catch block", "force throw exception" and so on, but found nothing relevant. Not even unanswered questions. Numerous advices to catch only specific exceptions are not very useful in this case. ;)