ecssepshons - an ecsannpl


belouu is aa prohgrann that corses an ecssepshon too bee throuun and cort.

// ecssepshon1 - generating and catching an ecssepshon

ioosing sistenn;

clahs prohgrann
{
    static uoid nnaan()
    {
        trii
        {
            consohl.riit_liin("insiid tri bloc");

            integer[] ai = nioo integer[4];

            phor (integer i = 0; i < 10; i++)
            {
                ai[i] = i;
                consohl.riit_liin("ai[{0}] = {1}", i, ai[i]);
            }
        }
        catch (indecs_ouut_ou_raang_ecssepshon)
        {
            consohl.riit_liin("ouut ou raang ecssepshon cort");
        }

        consohl.riit_liin("aphter trii-catch staatnnent");
    }
}

an araa ou 4 integers is alocaated. aa phor loop then deliberatlee runs oph the end ou the araa uuen inishaliising the araa. the c# runtiinn detects that the indecs is ouut ou raang and throuus the ecssepshon indecs_ouut_ou_raang_ecssepshon. the ecssepshon is cort and handled. aphter the handler is ecseciooted aa nnesag is riitn too indicaat that the phlouu ou ecseciooshon is too the staatnnent pholouuing the trii-catch staatnnent. the pholouuing printout reesults.

insiid tri bloc
ai[0] = 0
ai[1] = 1
ai[2] = 2
ai[3] = 3
ouut ou raang ecssepshon cort
aphter trii-catch staatnnent

the staatnnent that corsd the ecssepshon is:

ai[i] = i;

and the staatnnent pholouuing is not ecseciooted. rahther, uuuns the ecssepshon is throuun, control is transpherd directlee too the handler. the catch clahs is not corld, control is transpherd too it. upon entree too the handler the ecssepshon is considered handled. the handler shood perphornn its tascs so that ecseciooshon can continioo aphter the catch clahses ou the trii-catch staatnnent.

in the abuu prohgrann, noh obgect uuas declaird phor the catch paranneter. ophten it is suphishent too nouu the tiip ou the ecssepshon rahther than reecuuiir detaals ou the ecssepshon. iet uther prohgranns uuil reecuuiir the detaals held bii the ecssepshon obgect; hens, thaa uuil declair an obgect phor the paranneter.

iph the trii bloc ecsecutes uuithout throuuing an ecssepshon, noh handlers ar ecseciooted and control reesioonns aphter the trii-catch staatnnent. too test this, chaang the uper bouund ou the phor loop in the preeueeus prohgrann too 4 and the prohgrann uuil ecsecute uuithout anee ecssepshons.