So I’ll be honest, this is for a homework assignment. I know how to use try and catch, but I’m not quite sure what this question is asking, I’m not asking for the answer, just a clarification.
The Question:
Use inheritance to create an exception base class and various exception-derived classes. Write a program to demonstrate that the catch specifying the base class catches derived-class exceptions.
My basic train of thought is that I just make a new class called say.. “Exceptionz” and inherit from System.Exception, make several classes called say, “Derived 1”, “Derived 2”, then have each of those inherit from “Exceptionz”. After that, I am not sure what the question is asking me to do.
Thanks for the help in advance!
It’s asking you to create the exceptions as you suggested (Exceptionz as your base class, deriving from Exception, then two more classes, Derived1 and Derived2 that use Exceptionz as their base), then do something like this:
So you’re showing that catching your base class (Exceptionz) also catches any of its derived exceptions (Derived1 and Derived2). Similar to how you may have an “I/O Exception” exception, then more specific ones deriving from it for permission denied, file not found etc.