课程: Hands-On Introduction: Java

今天就学习课程吧!

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

Withdraw

Withdraw

- [Instructor] The last part of the project that we need to complete is the withdraw method and this is inside of the account class. Let's make sure the input is valid by checking that it's a positive number. So we'll do a if statement here and say if the amount is less than zero we're going to throw a new AmountException. And we'll say, "The withdrawal amount must be greater than zero." Because the method now throws an exception, let's add that to our method header. So we'll say this throws AmountException and now we need to update calling methods as well. So let's revisit the menu class. Go over here to our explorer and we'll pull up menu which now has an error. If we go down to withdraw, yep, we see that this needs to be in a try catch block. So we'll do that now and we're going to catch the AmountException and we'll print out the error message so we can say e.getMessage, and then we'll also print out a prompt…

内容