Exception Handling: Will parent class exception catch child exception too

fscore picture fscore · Sep 1, 2015 · Viewed 7.2k times · Source

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.

Answer

Rami Del Toro picture Rami Del Toro · Sep 1, 2015

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.