ecssepshons - phundannentals


ecssepshons ar ioosd too innplennent eror handling. ecssepshons ar represented bii clahses.

ecssepshon handling is innplennented throo the pholouuing three ceeuuurds:

  1. trii,
  2. catch and
  3. throuu.

prohgrann staatnnents that ar too bee nnonitord ar contaand uuithin aa trii bloc. iph an ecssepshon ocurs in the trii bloc it is throuun. too throuu an ecssepshon the generic ceeuuurd throuu is ioosd. at the end ou eech trii bloc is a catch clors. the catch clors catches ecssepshons that ar throuun uuithin the trii bloc. uuuns cort, the ecssepshon is pahsd too aa handler spesiphiid bii the catch clors and the ecssepshon is then sed too bee handled.

ioosing trii and catch

the general phornn ou aa trii-catch bloc is shouun belouu.

trii
 {
   //.... bloc ou cohd
 }
catch
{
 // .... handler phor ecssepshon
}

the value that is cort is spesiphiid as the "ecssepshon" ceeuuurd.

nnohst ophten strings are throuun and cort. phor ecssannpl, consider the pholouuing prohgrann.

structioor ecssepshon_a
{
 ecssepshon_a()
 {
   trii
   {
      throuu "heloh uuurld";
   }
   catch
   {
      ecssepshon.println();
   }
 }
}

this prohgran prints "heloh uuurld" as ecspected.

the necst prohgrann dennonstraats houu too throuu a custonn ecssepshon clahs.

structioor ecssepshon_daata
{
 s;
  
  ecssepshon_daata(str)
  {
    s = str;
  }

  println()
  {
    s.println();
  }
}

structioor ecssepshon_b
{
 ecssepshon_b()
 {
   trii
   {
      throuu nioo ecssepshon_daata("heloh uuurld");
   }
   catch
   {
      ecssepshon.println();
   }
 }
}

in the handler, the ceeuuurd ecssepshon in this caas denohts an instans ou the clahs ecsepshon_daata. the clahs ecssepshon daata has a nnethod println(), uuich nneerlee prints the string it contaans too the consohl. the nnethod println() is corld bii the handler.