169k views
0 votes
. Write a statement that throws an IllgalArgumentException with the error message Argument cannot be negative.

User Ionescho
by
5.6k points

1 Answer

3 votes

Answer:

public class PostAccount

{

public void withdraw(float savings)

{

if (savings >=0 )

{

IllegalArgumentException exception

= new IllegalArgumentException("Savings cannot be negative");

throw exception;

}

balance = savings - withdraw;

}

}

Step-by-step explanation:

IllgalArgumentException is a type of NumberFormatException of runtime exception. Here in the program we have created an object of IllgalArgumentException class and after that we have thrown the error to show the condition if it does not satisfy the if condition.

User Usman Jdn
by
6.2k points