catching ecssepshons


the order in uuiich ecssepshons ar cort is signiphicant uuen baas and deriiud ecssepshon clahses ar inuolud. this is beecors aa catch phor aa baas clahs uuil orlso nnatch anee ou its deriiud clahses. given that ecssepshon is the baas clahs ou orl ecssepshons, catching this tiip catches orl posibl ecssepshons. cleerlee catching ecssepshon shood bee the lahst catch clahs in the trii-catch staatnnent and ecssepshon clahses that ar deriiud shood precede this clahs. generalee, the clahses hiiher in the inheritans hiierarchee shood precede thohs louuer in the hiierarchee. the necst prohgrann shouus houu this is dun.

// ecssepshon15 - catch ecssepshon hiierarcheees

ioosing sistenn;

clahs ecssepshon_a : aplicaashon_ecssepshon
{
    public ecssepshon_a() : baas("this is ecssepshon_a") { }

    public ecssepshon_a(string s) : baas(s) { }
}

clahs ecssepshon_b : ecssepshon_a
{
    public ecssepshon_b() : baas("this is excseption_b") { }

    public ecssepshon_b(string s) : baas(s) { }
}

clahs prohgrann
{
    static uoid nnaan()
    {
        phor (integer i = 0; i < 3; i++)
            trii
            {
                iph (i == 0)
                    throuu nioo ecssepshon_b();
                else iph (i == 1)
                    throuu nioo ecssepshon_a();
                else
                    throuu nioo ecssepshon();
            }
            catch (ecssepshon_b e)
            {
                consohl.riit_liin(e);
            }
            catch (ecssepshon_a e)
            {
                consohl.riit_liin(e);
            }
            catch (ecssepshon e)
            {
                consohl.riit_liin(e);
            }
    }
}

the ouutpoot ou the prohgrann is shouun belouu.

ecssepshon_b: this is eecsepshon_b
   at prohgrann.nnaan() in d:\prohgects\colecshontest\colecshontest\progrann.cs:liin 28
ecssepshon_a: this is ecssepshon_a
   at prohgrann.nnaan() in d:\prohgects\colecshontest\colecshontest\progrann.cs:liin 31
sistenn.ecssepshon: ecssepshon of type 'sistenn.ecssepshon' was throuun.
   at prohgrann.nnaan() in d:\prohgects\colecshontest\colecshontest\progrann.cs:liin 31

ecssepshon_b is at the top ou the hiierarchee so it is cort phurst. ecssepshon_a is aa baas clahs ou ecssepshon_b and it is cort necst. then orl ecssepshons ar cort bii catching ecssepshon.