课程: Hands-On Introduction: Java

今天就学习课程吧!

今天就开通帐号,24,700 门业界名师课程任您挑!

Deposit

Deposit

- [Instructor] We've provided users with a menu option to deposit funds into their account. Let's provide the backend implementation to make that happen within the Account class. Because the amount is input provided by the user we want to do a bit of error checking to make sure that the input is valid. For example, let's make sure the amount is not zero or a negative number. We can do so by writing an if statement and saying if the amount is less than one do something. If this happens to be the case we shouldn't continue any further. We can stop the program by throwing an exception. To throw an exception we'll write the word throw followed by new and the name of the exception. Now, Java provides a lot of built-in exception classes that you can use, or you can make your own custom exceptions. For this error let's throw our own custom exception. So, this exception we'll call it AmountException, and we'll set a message in…

内容