0

Why 2 times exception is caught

Hi Please refer code below: Why it shows both exception statement as output ? Only exception inside b is not sufficient to handle the throw statement? https://code.sololearn.com/c844YSckt9AK/?ref=app

26th Nov 2023, 4:11 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
5 Respostas
+ 1
Because B wss not initialised successfully. You tied it directly to A. A throws, B throws. All goes to main. You end up with 2 error messages. The initialisation did not result in anything useful. It just passed the exception to main. try this: https://code.sololearn.com/cKB9fa9cIQ23/?ref=app
26th Nov 2023, 11:13 AM
Bob_Li
Bob_Li - avatar
+ 1
Thanks Bob_Li
26th Nov 2023, 12:52 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
0
I belive this is different than my usecase. It says that exception is thrown to next level until it is caught in call hierarchy. However, I am actually catching it as first place in class B constructor it self. Had I been having B constructor as follow, B() : Pa(new A){} A's exception will not be handled by B class and will be available to main function which created object of B. My case is having handled exception in B , then why it reaches to main function ?
26th Nov 2023, 11:03 AM
Ketan Lalcheta
Ketan Lalcheta - avatar