Welcome to the CSC Q&A, on our server named in honor of Ada Lovelace. Write great code! Get help and give help!
It is our choices... that show what we truly are, far more than our abilities.


+17 votes

I'm confused about this in part 4 of the study guide: "Creating & throwing a custom Exception subclass." How do you do this? Are there any resources on moodle for this? Thanks!

asked in CSC305 Fall 2022 by (1 point)

3 Answers

+9 votes

All you have to do is create a new class and give it an exception name that has the word "Exception" in it and make that class extend the Exception class or the RuntimeException class.

answered by (1 point)
+9 votes

For better example, you could take a look at the .java files (LowAccountBalanceException.java and BankAccount.java) uploaded on Moodle during week October 31 - November 6 for better example of how to throw a custom Exception class in BankAccount.

Hope this helps!

answered by (1 point)
+8 votes

Create a class and extend the Exception class. Create the new exception and give it your own message to throw or get the message from the Exception class.

answered by (1 point)