edle
edle

Reputation: 21

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!

Upvotes: 1

Views: 3551

Answers (7)

erickson
erickson

Reputation: 269637

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!");
}

Upvotes: 3

Baleroc
Baleroc

Reputation: 167

You can do this by creating a custom Exception that you can create yourself.

  • These exceptions can either be Checked exceptions, that are enforced by Java compiler (Requires try/catch or throws to implement)
  • Or the exception can be Unchecked exception, which throws during runtime, that are not enforced by the Java compiler.

Based on what you have written, it seems you want an Unchecked exception that is not enforced, but throws an error during runtime.

One way of doing this is through the following:

public class CustomException extends RuntimeException {
       CustomException() {
              super("Runtime exception: problem is..."); // Throws this error message if no message was specified.
       }

       CustomException(String errorMessage) {
            super(errorMessage); // Write your own error message using throw new CustomException("There was a problem. This is a custom error message"); 
       }
}

Then in your code, you could do the following:

public class Example {
    String name = "x";
    if(name.equals("x"))
          throw new CustomException();   // refers to CustomException()
}

Or

 public class Example2 {
        String name = "y";
        if(name.equals("y")) 
             throw new CustomException("Error. Your name should not be this letter/word."); // Refers to CustomException(String errorMessage);
 }

You can also do this for Throwable as well.

Upvotes: 0

Eagle'sNest
Eagle'sNest

Reputation: 106

I'm not sure I understand your question fully, but if you just add "throws Exception" to the method header and throw the exception in the method where it should fail, that should work.

Example:

public void HelloWorld throws Exception{
    if(//condition that causes failure)
        throw new Exception("Custom Error Message");
    else{
        //other stuff...
    }
}

Upvotes: 0

Dima Maligin
Dima Maligin

Reputation: 1482

Just do:

try {
    ...
} catch (Exception e) {
    System.err.print("what ever");
    System.exit(1); // close the program
}

Upvotes: 1

Leonardo Kenji Shikida
Leonardo Kenji Shikida

Reputation: 751

You can't, except if you get openJDK, change the sources and recompile.

However, what most developers usually do is to use some logging library such as log4j and use different detail levels, according to the logging settings.

So you can print the full stacktrace using a lower level such as TRACE or DEBUG and a more human-readable message in the ERROR or WARN (or even INFO) levels.

Upvotes: 0

BritishKnight
BritishKnight

Reputation: 181

When you construct an exception object, one of the constructors will take a String object of the message.

Upvotes: 0

Pritam Banerjee
Pritam Banerjee

Reputation: 18923

This is how you do it :

try{
   //your code
 }catch(Exception e){
   System.out.println("Whatever you want to print" + e.getMessage());
   System.exit(0);
 } 

Upvotes: 0

Related Questions