简体   繁体   中英

If my parent class throws an exception, must my child class catch it?

I have a parent class which throws some exceptions, not handling those exceptions in the parent class. And now I am inheriting this parent class. Is it necessary to catch exceptions thrown by my parent class?

You should catch all of your exceptions where they occur. Where you think that a code could throw an exception make a try-catch statement and handle the exception. you don't want to have exceptions and do not handle them.

Gotta catch 'em all!

覆盖的方法只能抛出其覆盖的父方法抛出的异常或期望的子类。

If your parent class throws an Exception then your child class may either throw the exception or you can also catch the exception appropriately in the over-riding method.

Overridden methods can throw only the subclasses of exception thrown by the method in superclass, and it cannot throw the superclass of exception thrown by the method in superclass.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM