Java Best way to throw exception in a method

前端 未结 3 1918
误落风尘
误落风尘 2021-01-26 00:28

I have created my own type of exception and want to implement it in a method. As of now I have written it in the following way, and it works.

public Worker remov         


        
3条回答
  •  长发绾君心
    2021-01-26 00:43

    In case you have some other information other than error message to be sent out along with the exception.

    You will need to first create the Object of the Exception

    set values you want to throw in the Exception

    Or you can write your own constructors for the Exception which would take different values and exception message to create object to the Exception you wanna throw.

    As such

    throw new NoSuchEmployeeException(/*"Employee could not be found"*/);
    

    is fine

提交回复
热议问题