मान लें कि आपके पास निम्न पदानुक्रम है। तुम्हें पता है, बिल्ली, माउस, कुत्ता की तरह उप वर्गों के एक समूह के साथ, एक आधार वर्ग पशु है आदिआप एक ही मामले में एकाधिक पकड़ ब्लॉक के माध्यम से अपवाद हैंडलिंग कैसे कर सकते हैं?
अब, हम इस परिदृश्य है:
void ftn()
{
throw Dog();
}
int main()
{
try
{
ftn();
}
catch(Dog &d)
{
//some dog specific code
}
catch(Cat &c)
{
//some cat specific code
}
catch(Animal &a)
{
//some generic animal code that I want all exceptions to also run
}
}
तो, क्या मैं चाहता हूँ वह यह है कि भले ही एक कुत्ता फेंक दिया जाता है, मैं कुत्ते को पकड़ने का मामला निष्पादित करना चाहता हूं, और पशु पकड़ने का मामला निष्पादित करना चाहता हूं। आप यह कैसे करते हैं?
void ftn(){
throw Dog();
}
int main(){
try{
try{
ftn();
}catch(Dog &d){
//some dog specific code
throw; // rethrows the current exception
}catch(Cat &c){
//some cat specific code
throw; // rethrows the current exception
}
}catch(Animal &a){
//some generic animal code that I want all exceptions to also run
}
}
सबसे स्पष्ट सामान कभी-कभी मुझे बढ़ाता है। :) – Xeo