简体   繁体   中英

Exception Handling: Will parent class exception catch child exception too

SAXException is extended by SAXNotRecognizedException , SAXNotSupportedException .

try {
  //do Something
} catch(SAXException e) {}
  catch(ParserConfigurationException pce) {}

if lets say 'do something' has some code that throws SAXNotRecognizedException or SAXNotSupportedException and if that happens then nothing should happen. If SAXNotRecognizedException occurs then will nothing happen since its a empty block. Similarly for ParserConfigurationException. Right now, even though I say do nothing for ParserConfigurationException then also a exception for ParserConfigurationException is thrown.

I basically want it to do nothing when SAXNotRecognizedException or SAXNotSupportedException occurs.

Yes parent exceptions will catch child exceptions as well. A known example is when you catch Exception . This try block will catch all sorts(child) exceptions such as NullPointer etc.

If you like to catch only SAXNotRecognizedException or SAXNotSupportedException , a better way to do it since Java 7 is:

try {
  // Do something
} catch(SAXNotRecognizedException | SAXNotSupportedException e) {
  // Do nothing
}

Note, other SAXExceptions won't be caught.

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