I'm a little confused because I want to be able to throw a custom exception in Java. To do this inside a function, I have to add throws MyException
to the header of the function.
And then anything that calls that function in turn must add throws MyException
or have a try-catch block. But why is this?
For example, when creating a Stack in java and calling the pop function, I don't have to have a try-catch and yet the pop method in Java throws a NoSuchElementException
(or w/e it is) if there isn't an element on the stack.