1

I am doing one android application, in this app I am creating UI fields dynamically by local db. While creating fields based on response from remote server I am getting some null pointer exception due to some fields are not found in db. But, I handle these exceptions by using try catch block. Is these exceptions cause to lower performance of app? If yes then give me any other alternatives to handle this.

here is my code-

@Override
public void populateField() {


    String value = "";
    try {
        value += DBHelper.getInstance().getDataByID(mField.getId(), 
          refCode).getFieldValue();// **here getting exception**
     .
     ....  
    } catch (Exception e) {
        Logger.ex(e);
    } 
sachin
  • 153
  • 4
  • 18

2 Answers2

1

The true cost of using try/catch very much depends on circumstances (mainly the JVM implementation). See here for example.

Yes, obviously, when you have the catch block kick in, quite some CPU cycles will be used for handling it.

But beyond that, Java is simply a language that prefers LBYL over EAPL (see here for example). Meaning: you look before you leap, instead of asking for foreignness instead of permission.

These two things combined tell you to avoid unnecessary try/catch blocks. In other words: if it is to be expected that "values" are null, then add code that checks whether the value is null or not. Use if, not try/catch!

GhostCat
  • 137,827
  • 25
  • 176
  • 248
0

If you are not planning to use (read, recuperate from end user) the logs on the application, you should note call

 Logger.ex(e)

depending on the implementation of that logger it might write (or try to write) the exception to a file, which might take some time.

T.S.
  • 29
  • 7