I have an app that uses custom Exceptions, such as this:
public class SomeException extends Exception{
private int iCode;
private String iMessage;
public SomeException(){
iCode = 201;
iMessage = **//Get the localized string R.string.error_201??**
}
@Override
public String getMessage() {
return iMessage;
}
@Override
public int getCode() {
return iCode;
}
}
Obviously, I want lo localize the error message. I have possible solutions but non of them satisfy me.
1) Pass "Context" to the constructor, and do ctx.getString(R.string.error_201) --> Fail, as this Exceptions are sometimes thrown from MODEL classes, so they don't have a Context
2) Pass "Context" when retriveing the message in getMessage() function, --> Fail, It's necesary to override the super method, to work as all other Exceptions.
Solution I have now: All activities in my app have this onCreate:
public void onCreate(...){
Utils.RESOURCES = getResources();
...
}
Very dirty code... I don't like the solution. My question is then,: is there a way to access the resources without the Context? And most important, How would an application such as mine solve this problem?
In the above example, exceptions can be thrown from the try-with-resources statement when: The file test.txt is not found. Closing the BufferedReader object. An exception can also be thrown from the try block as a file read can fail for many reasons at any time.
Though an exception is thrown while closing a particular resource, all opened resources will be closed irrespective of the thrown exception. In our example, though there was an exception while closing Foo resources, Bar resource was closed successfully as well.
Before transferring the control to catch method to handle the exception, resources are closed by invoking their respective close () methods. The close () of Foo class prints the message, “Closing Foo” to console and throws an exception which gets suppressed as the exception thrown by the try block gets exposed.
If exceptions are thrown from both the try block and the try-with-resources statement, exception from the try block is thrown and exception from the try-with-resources statement is suppressed. In Java 7 and later, the suppressed exceptions can be retrieved by calling the Throwable.getSuppressed () method from the exception thrown by the try block.
What about
public class MyException extends Exception {
private int iCode;
public MyException(int code) {
this.iCode = code;
}
@Override
public String getMessage() {
return "MyException code " + String.valueOf(iCode);
}
public String getLocalizedMessage(Context ctx) {
String message;
if (iCode == 201)
message = ctx.getString(R.string.error_201);
else if (iCode == 202)
message = ctx.getString(R.string.error_202);
// ...
}
}
Even if there was way to access context in different way, you should not do it. If you need to emit exceptions where you cannot pass Context, you should be able to access context before you display such error. I cannot see reason why you should create localized error messages from constructor. You can log to logcat not localized versions if you need. And where you want to display something in UI, you should have context at hand.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With