2
.
16/4 4 32/4 8
.
16
.
' .
"" , , ' catch.
. , , , . " , , , . , - . , " , .
try
try . , try catch, try. , IndexOutOfRangeException , try.
// try.
Using System;
class NestTrys {
static void Main() {
// numer denom.
int[] numer = { 4, 8, 16, 32, 64, 128, 256, 512 };
int[] denom = { 2, 0, 4, 4, 0, 8 };
try { // try
for(int i=0; i < numer.Length; i++) {
try { // try
Console.WriteLine(numer[i] + " / " +
denom[i] + " " + numer[i]/denom[i]);
}
catch (DivideByZeroException) {
Console.WriteLine(" !");
catch (IndexOutOfRangeException) {
Console.WriteLine(" .");
Console.WriteLine(" - .");
}
}
}
.
4/2 2 !
16/4 4 32/4 8 !
16 .
- .
, try - , . , try, .
, try, . try . , , . , try -. , try . , try "" , .
|
|
, . throw. :
throw exceptOb;
exceptOb , Exception.
, throw DivideByZeroException.
// .
Using System;
class ThrowDemo {
static void Main() { try {
Console.WriteLine(" ."); throw new DivideByZeroException();
}
catch (DivideByZeroException) {
Console.WriteLine(" .");
}
Console.WriteLine(" try/catch.");
}
}