2

Using java, I want to do something like

try {
  someMethod();
} catch (Throwable x) {
  x.setMessage("Some comment " + x.getMessage());
  throw x;
}

That is, I do not know what "someMethod" will throw. Whatever it is, I want to add a message at the beginning of its message, and then throw the error. But Throwable does not have a setMessage() method.

I can do a Class<? extends Throwable> cls = x.getClass(); to get the class type, but I am not sure of the syntax. I can't do a throw new cls("Comment " + x.getMessage()); I am sure there must be a fairly simple way to do this, when you don't know the class of the throwable that is thrown.

2
  • 2
    Is there a reason not to use the more common pattern of catch (Throwable t) { throw new CustomExceptionOrSomething("Some comment", e); }? Commented Oct 8, 2018 at 19:47
  • It's not recommended to catch Throwable, since those could be JVM-level conditions (e.g., OutOfMemoryError). In almost all cases, you'd be better off catching Exception instead. Commented Oct 8, 2018 at 21:00

2 Answers 2

2

Instead of catching Throwable (which is almost always a mistake), you can create a custom RuntimeException and wrap your caught exception in that.

public class MyException extends RuntimeException {
    public MyException(String message, Throwable cause) {
        super(message, cause);
    }
}

try {
    someMethod();
} catch(Exception e) {
    throw new MyException("A major error has occurred!", e);
}
Sign up to request clarification or add additional context in comments.

3 Comments

Or just wrap it directly in a RuntimeException.
Actually it occurs to me I need them to be assertion errors because I want the test (this is with Selenium) to continue even if a specific part fails. I have a List<AssertionError> errors = new ArrayList<>() to which I will add each error thrown and then throwIfNecessary() at the end
I will supplement the answer above, that you can also create your own custom exception, inheriting not only from RuntimeException but also from Exception class
0

You can simply catch the Exception thrown by someMethod() and then re-throw it with the message you want.

Something like,

try {
   someMethod();
} catch(Exception ex) {
   /* Optional: Log Error */
   Logger.error(..., ex);
   throw new Exception("Error Occurred While Processing Request.", ex);
}

If you want you can also create and throw a checked exception as follows,

CustomException Class:

public class CustomException extends Exception {
    /* Optional: Add Serial UID */

    public CustomException(String message, Throwable cause) {
        super(message, cause);
    }

    public CustomException(Throwable cause) {
        super(cause);
    }
}

Code:

try {
   someMethod();
} catch(Exception ex) {
   /* Optional: Log Error */
   Logger.error(..., ex);
   throw new CustomException("Error Occurred While Processing Request.", ex);
}

1 Comment

I think I will try user2004685's suggestion since I can then add the exception to the array

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.