-
Notifications
You must be signed in to change notification settings - Fork 2
/
90.cpp
37 lines (30 loc) · 881 Bytes
/
90.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include<iostream>
using namespace std;
class Base
{
};
class Derived : public Base
{
};
int main()
{
//instantiating an object of Derived class
Derived d;
try
{
//throwing exception of derived class
throw d;
}
catch(Base B) //catching an exception of base class
{
cout<<"Exception of base class handled"<<endl;
}
catch(Derived D) //catching an exception of derived class
{
//this block is never handled because the exception is been handled by the catch block of base class
cout<<"Exception of derived class handled"<<endl;
}
//also a warning has been given because the base class catch block handles the exception before the derived class catch block
//For better practice, always use catch block of derived class followed by the catch of its base class
return 0;
}