Throwing multiple exceptions in Java

后端 未结 11 1464
无人及你
无人及你 2020-12-28 13:04

Is there any way to throw multiple exceptions in java?

11条回答
  •  天涯浪人
    2020-12-28 14:03

    You can have the possibility of throwing multiple different exceptions. For example:

    if (obj == null)
        throw new NullPointerException();
    
    if (some other case)
        throw new IllegalArgumentException();
    
    if (this == this)
        throw new IOException();
    

    This code may throw multiple different exceptions, but this can never happen at the same time.

提交回复
热议问题