Why should we write custom exception classes in Java

前端 未结 6 1672
执念已碎
执念已碎 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:34

    You need to have your client code know what exact exception happens by which part of code. so you need to let exception semantic and distinguished from other code block.

    How to do this:

    1. Define new exception class, so the class name tells what happens
    2. Define a unifed/generic exception class which wraps code, message or other info. the code can tells what happens.

    To summarize it, Do something let your exception have some meaning/semantics, and let its client know what exactly happens.

提交回复
热议问题