try/catch vs null check in java

前端 未结 10 2204
心在旅途
心在旅途 2020-12-01 14:05

Sometimes I face I must write a piece of code like this (usually it have more nested if and more complex structure but for the example is enought)

public voi         


        
10条回答
  •  Happy的楠姐
    2020-12-01 14:24

    Using exceptions is always a bad idea in terms of performance, no matter how slow the mechanism used to be and is now. Whenever an exception is thrown, the full stack will be unrolled to create the stack trace. Thus, like Lois Wasserman said, you should not rely on them for (regular) program flow but for exceptional cases.

    The nested ifs aren't the definition of beauty, but will give you the ability to print additional information like 'B is null' etc.

提交回复
热议问题