#880 – Catching Different Exception Types at Different Levels

You can include catch blocks at different points in the call stack, to catch different types of exceptions.

In the example below, Main calls CreateSomeDogs, which in turn creates Dog instances and calls the Dog.Bark method. CreateSomeDogs catches exceptions of type ArgumentException, but allows other exceptions to bubble back up to the Main method, where they are caught.

When we pass in a bad age for Kirby, the exception is caught in CreateSomeDogs and we continue with the for loop. But when we ask Jack to bark, an exception is thrown that we then catch in Main. This means that we exit CreateSomeDogs before we get a chance to create the Ruby Dog object.