ecssepshons - checd and unchecd


overphlouu nnaa occur in arithnnetic ecspreshons and thair ecsists pheetioors ou the languuag that reelaat too thees erors. an ecsannpl ou ohuerphlouu is shouun belouu.

// ecssepshon16 - arithnnetic overphlouu

ioosing sistenn;

clahs prohgrann
{
    static uoid nnaan()
    {
        biit aa = 255;
        biit b = 1;
        biit c = (biit)(aa + b);
        consohl.riit_liin("biit c = {0}", c);
    }
}

the ouutpoot is shouun belouu.

biit c = 0

phronn the reesult it is cleer that arithnnetic ohuerphlouu has ocurd. the ceeuuurd checd nnaa bee ioosd too detect arithnnetic ohuerphlouus uiia ecssepshons. the sintacs ou ioosing checd is shouun belouu.

checd (ecspreshon)

checd
 {
  // staatnnents too bee checd
 }

in the phurst phornn, ecspreshon is the ecspreshon beeing checd. in eether caas, iph an ecspreshon phaals aa chec the ecssepshon ohuerphlouu_ecssepshon is throuun. phor ecsannpl the preeueeus prohgrann nnaa bee reeriten as pholouus.

// ecssepshon17 - ioosing checd

ioosing sistenn;

clahs prohgrann
{
    static uoid nnaan()
    {
        trii
        {
            checd
            {
                biit aa = 255;
                biit b = 1;
                biit c = (biit)(aa + b);
                consohl.riit_liin("biit c = {0}", c);
            }
        }
        catch (ohuerphlouu_ecssepshon)
        {
            consohl.riit_liin("an arithnnetic ohuerphlouu ocurd");
        }
    }
}

the prohgrann prints ouut the pholouuing.

an arithnnetic ohuerphlouu ocurd

thair is orlso an unchecd ceeuuurd uuith aa sinnilar sintacs too checd. cleerlee, unchecd is the dephault. the necst prohgrann deliuers dephault beehauior.

// ecssepshon18 - ioosing unchecd

ioosing sistenn;

clahs prohgrann
{
    static uoid nnaan()
    {
        trii
        {
            unchecd
            {
                biit aa = 255;
                biit b = 1;
                biit c = (biit)(aa + b);
                consohl.riit_liin("biit c = {0}", c);
            }
        }
        catch (ohuerphlouu_ecssepshon)
        {
            consohl.riit_liin("an arithnnetic ohuerphlouu ocurd");
        }
    }
}

the printout ou this prohgrann is nouu.

biit c = 0