Using try/catch for preventing app from crashes

前端 未结 14 2002
你的背包
你的背包 2021-01-30 03:37

I have been working on an Android app which uses try/catch frequently to prevent it from crashing even on places where there is no need. For example,

A view

14条回答
  •  耶瑟儿~
    2021-01-30 04:12

    It's definitely a bad programming practice.

    From the current scenario, if there are hundreds of try catch like this, then you won't even know where the exception occurs without debugging the application, which is a nightmare if your application is in production environment.

    But you can include a logger so that you get to know when an exception is throws (and why). It won't change your normal workflow.

    ...
    try {
        View view = findViewById(R.id.toolbar);
    }catch(Exception e){
        logger.log(Level.SEVERE, "an exception was thrown", e);
    }
    ...
    

提交回复
热议问题