Why should we write custom exception classes in Java

前端 未结 6 1790
执念已碎
执念已碎 2020-12-19 03:49

What is the purpose of writing custom exception classes when mostly what it does is same. For eg, NullPointerException:

class NullPointerException extends Ru         


        
6条回答
  •  长情又很酷
    2020-12-19 04:14

    It is basically to Handle different Exception in different ways. Say, you might want to do some different operation on ArrayIndexOutOfBoundsException than a NumberFormatException.

    or more clearly

    }catch (ArrayIndexOutOfBoundsException ex){
    //operation 1
    }catch (NumberFormatException ex){
    //operation 2
    }
    

提交回复
热议问题