1

I am trying to intercept an Exception thrown by my algorithm in Java, and change the text to another language, and show it to the user. Is this possible in java?

Sorry by my English.

James
  • 338
  • 1
  • 2
  • 16

4 Answers4

1

You can catch an exception, and then throw a new one of the same type (or a different type if you prefer). Then translate the message from the original exception and pass it into the new exception. Something like the following (not tested):

try {
  // do something
}
catch (Exception e) {
  throw new Exception(translate(e.getMessage()))
}

public String translate(String message) { /* translation code */ }

Update in response to comment:

If you can modify the application (and particularly if the application uses custom exceptions derived from a few base classes) then you might be able to modify these to retrieve translated messages (but if you could do that, you could build in the translated messages, or full internationalisation, from the start!).

Intercepting and replacing all Exceptions globally is probably not possible, although it looks like you can at least detect them globally using debug hooks - see this answer. It's conceivable that you could modify the Exception message on-the-fly using reflection, but this sounds pretty nasty.

One final possibility is to use Aspect-Oriented Programming (AOP) to compile-in extra behaviour around code that throws an exception (see this question, for example). You'd need to recompile the whole application using AOP tools though, and would probably still need to modify the application code a bit.

Community
  • 1
  • 1
DNA
  • 42,007
  • 12
  • 107
  • 146
  • Thanks DNA, but i am looking for something different, i wanna to intercept all exceptions thrown by my application in Runtime and translate it. i will take the new text from a XML and de original text is the key or tag to get it. Did you understand? – Rodrigo Almeida Mar 03 '15 at 16:39
0

I think you want Thread.setDefaultUncaughtExceptionHandler() This issue had more details if you need them... swing uncaughtexceptionhandler

Community
  • 1
  • 1
Emily Crutcher
  • 638
  • 5
  • 10
0

You would need to translate the message text of an exception only if your were reporting the message text of exceptions. But doing that is a mistake.

  • The message was created when the exception was thrown. It therefore at best can provide only very low level information, which can be inappropriate for reporting to a user.
  • Philosophically, using the message is against the whole point of exceptions, which is to separate the detection and initiation of error handling (the throw part) from completion of handling and reporting (the catch part). Using the message means the message must be good for reporting, which moves responsibility for reporting to the location that should be responsible for only detection and initiation. That is, I'd argue that the getMessage() part of the design of Throwable was a mistake.

Instead of doing that, follow the advice to choose what message to report to the user at the point where your catch the exception. You can internationalize that message use the usual Java text internationalization facilities.

Raedwald
  • 46,613
  • 43
  • 151
  • 237
-1

I solved my question with this solution

public class ParamsException extends RuntimeException {
private static final long serialVersionUID = 7161500238672497882L;

public ParamsException() {
    super();
}

public ParamsException(String message) {
    super(new BaseResourcesBusiness().getException(message));
}

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

public ParamsException(String message, Throwable cause) {
    super(new BaseResourcesBusiness().getException(message), cause);
}

}

the BaseResourceBusiness.getException(message) get the message from a XML or Database to a specific language. So all my exception is created in this mode

public static final class NotFoundInDatabaseException extends ParamsException {
    private static final long serialVersionUID = -1595240938885629473L;

    public NotFoundInDatabaseException () {
        super("Not found in database");
    }
}

in the code im using with this mode throw new NotFoundInDatabaseException ()