YUHEE1984 / CatsOneday

1 stars 0 forks source link

06 - Errors #6

Closed TheBeege closed 2 months ago

TheBeege commented 3 months ago

Goal

Use errors to ensure things make sense.

Background

This one's a little easier :)

Currently, there's nothing stopping a male cat from breeding with another male cat. Biology doesn't work this way, so we need to make sure our cat breeding systems prevents this from happening.

We'll do this with errors. If a male cat attempts to breed with a male or if a female attempts to breed with a female, the system should reject that. This is an exceptional behavior: it is an exception to the normal way things work and shouldn't be allowed. So we'll define an exception object to represent this kind of issue. We'll then define some logic to trigger that exception.

Steps

Defining the Exception

  1. In your org.yuhee.catsoneday package, create a new file and class: BreedingSexException. This new class should be public. It should inherit from Exception.
  2. Create a new public constructor for BreedingSexException. It should take in two Cat parameters. Name them as you like.
  3. In the constructor, call the super-constructor. It takes in a String parameter. This parameter is the error message. Set the parameter to specify that the first Cat and second Cat are of the same sex. Maybe include their names to help with future debugging.

Now, we have an exception object that we can use to handle behavior that breaks the rules. Well done!

Throwing the Exception

  1. In your Cat class, find the breed method.
  2. In the method signature, add throws BreedingSexException after the parameters but before the {.
  3. Before calling handleBreeding, use a condition to check if the sex of both Cats is the same. (Don't forget that you use .equals() with strings instead of ==!)
  4. If they're the same, throw a new BreedingSexException.

Our code now uses the exception, enforcing the rules of our application. If a programmer tries to do something that isn't allowed, this exception will prevent the program from making a mistake.

Handling the Exception

  1. Back in your App.java, you may be able to see that calling breed() now has a problem. We need to try and catch the exception. Wrap the call to breed in a try block.
    • Note that if you're declaring and initializing your ArrayList in a single line, you may need to split up declaration (ArrayList<Cat> someVariableName) and the initialization (someVariableName = someValue). Only the initialization should be inside the try block.
  2. In the catch block, catch the BreedingSexException.
  3. Print out to standard error (System.err.println()) the exception's message, using getMessage().
  4. After the printing the error message, the program shouldn't continue, so just return.
  5. Try running your program.
  6. Try changing your cats in the main method to have the same sex.
  7. Run your program. Notice the error printed and that the new cats from breeding aren't printed.
  8. Change the cats' sexes so that they can breed again.

After forcing proper handling of our exception, we should update our program's logic to handle the exception. We've demonstrated how we can prevent the program from breaking the rules and ensure things behave properly.

Defining exceptions can be a great way to ensure other engineers on your team don't try to use systems in ways they shouldn't. By defining a method that throws some exception, you can force other programmers to write logic to handle cases where their programs may break the rules. This helps you and your team avoid unexpected, exceptional behavior. Great job!

YUHEE1984 commented 2 months ago

<3