Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Resources in Exceptions

Tags:

android

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?

like image 335
Corbella Avatar asked May 22 '13 12:05

Corbella


People also ask

When to throw an exception in try with resources statement?

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.

What happens when an exception is thrown while closing a resource?

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.

How do you handle exceptions in control to catch method?

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.

How to get suppressed exceptions from try-with-resources in Java?

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.


1 Answers

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.

like image 160
Pihhan Avatar answered Nov 15 '22 07:11

Pihhan