What better do you do if you have to leave Exception with the comment?



  • There's a situation where I get the data from Intent, we have to deal with the NullPointerException, but if it comes down to the Exception, then we still need to break the league, because this behavior is unacceptable, but add a comment on why it happened, I do it in turn.

    try{
        mCPath = new CPath(intent.getStringExtra(EXTRA_CPATH)); 
    }catch(NullPointerException e){
        throw new NullPointerException("EXTRA_CPATH must be not null");
    }
    

    But my timlid says it's not so good, it's necessary to throw another exception, namely RuntimeException.

    try{
        mCPath = new CPath(intent.getStringExtra(EXTRA_CPATH)); 
    }catch(NullPointerException e){
        throw new RuntimeException("EXTRA_CPATH must be not null");
    }
    

    I don't quite understand the logic, in this context of the programme, in my personal opinion, if Null comes in, then the whale needs null, just supplement the comment or am I wrong?



  • (Extrapolating information from NET, I can make mistakes.)

    NullPointerException - It's a systemic exception that is generated in the subsoils of the Java virtual machine, which occurs when applied to an interior address, which is monitored at the operating system level. It's a stupid tone, and it's an exception, and throwing it away.

    You don't have to make an exception because the treatment of a zero object is a mistake in the logic of the application. If there's a variable or some field you've got null, you need to check that value, not expect the ranch to drop NPE.

    You don't have to face the exception because programmers can expect an exception to occur at a system level, not manually generated by a user code.

    What's the right thing to do in your case? Check. intent null, and if there is no value, leave the exception that is most logical, at least. IllegalStateException♪ If the meaning null came in the form of an argument, it should be abandoned. IllegalArgumentException♪ And there's no way to try-catch.


    Java seems to have different views on the use of an exception, but in general, my view prevails. See:

    • https://stackoverflow.com/a/3323006
    • https://stackoverflow.com/a/47710



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2