Search code examples
javaexceptionruntimeexceptionthrowable

Throwing an exception without "Exception in thread..."


I would like to know if there's a simple way of throwing an exception, but ONLY with the exact string I choose. I found a way to get rid of the stack trace, but now I want to remove the beginning of every exception:

"Exception in thread "main" RuntimeException..."

I'm looking for a simple, elegant way to do this (not super simple, but not way too complicated as well).

Thanks!


Solution

  • The correct way to do this is to set your own, custom, uncaught exception handler:

    public static void main(String... argv)
    {
      Thread.setDefaultUncaughtExceptionHandler((t, e) -> System.err.println(e.getMessage()));
      throw new IllegalArgumentException("Goodbye, World!");
    }