1. , (, ), ( ).
1
using System;
namespace ConsoleApplication
{
class OurClass
{
static void Main(string[] args)
{
float num1 = 1, num2 = 2, summarize, multiply, divide=0;
int k=0, l=0, d=0;
Console.Write(" num1:");
try { num1 = float.Parse(Console.ReadLine()); }
catch
{
Console.WriteLine(" !\n" +
" 1");
}
Console.Write(" num2:");
try { num2 = float.Parse(Console.ReadLine()); }
catch
{
Console.WriteLine(" !\n" +
" 2");
}
k = (int)num1;
l = (int)num2;
summarize = num1 + num2; multiply = num1 * num2; divide = num1 / num2;;
Console.WriteLine("num1 + num2 = " + summarize);
Console.WriteLine("num1 * num2 = " + multiply);
Console.WriteLine("num1 / num2 = " + divide);
Console.WriteLine(" \nk= " + k + " l= " + l);
try { d = k / l; }
catch (DivideByZeroException)
{
Console.WriteLine("d=k/l=?");
Console.WriteLine(" ! ");
Console.WriteLine(" 0");
}
Console.WriteLine("d=k/l="+d);
}
}
}
, . 1 , num2.
1:
2 , , , , .
2:
DivideByZeroException, .
3:
, . , . :
for (int i = 0; i < n+1; ++i),
i , 0 5. , 6, IndexOutOfRangeException. try, catch -:
catch (IndexOutOfRangeException)
{
Console.WriteLine(" ");
}
|
|
catch - .
2. .
2
using System;
namespace ConsoleApplication5
{
class Program
{
static void Main(string[] args)
{
const int n = 6;
int[] a = new int[n] { 3, 12, 5, -9, 8, -4 };
Console.WriteLine(" :");
for (int i = 0; i < n; ++i)
Console.WriteLine("\t" + a[i]);
Console.WriteLine();
int max = a[0];
try
{
for (int i = 0; i < n+1; ++i)
if (a[i] > max)
max = a[i];
}
catch (IndexOutOfRangeException)
{
Console.WriteLine(" ");
}
Console.WriteLine(" = " + max);
Console.Read();
}
}
}
:
, ? : , , :
throw
try . , try atch, , . .
. , , , , .
, #, . throw , . , System.tion. .
throw , :
throw [ ];
tch . , throw, , :
throw new DividZtion();
throw , 0 new. . , , throw, , tch, .
, throw DivideByZeroException.
3 throw
using System;
class Demo
{
public static void Main()
{
try
{
Console.WriteLine(" DivideByZeroException");
throw new DivideByZeroException();
}
catch (DivideByZeroException)
{
Console.WriteLine(" ");
|
|
}
Console.WriteLine(" ");
}
}
, - . , try, throw, new DivideByZeroException. try tch.
:
throw try :
Console.WriteLine(" ");
, .
tion (. 2), .
2 System.Exception
lpLink | URL |
ssage | . . |
Source | , |
StackTrace | , . |
InnerException | , |
TargetSite | , |
hecked unchecked. C# , , , , . :
byte i,j;
i=100; j=100; i=i+10*j;
, i byte.
:
- hecked ;
- unchecked , , ; .
, , , , :
= hecked (i=i+10*j); / /
checked
{ / /
i=i+10*j;
}
, OverflowException.
unchecked . , , , .
, .
6
1) ?
2) #?
3) ?
4) # ?
5) #.
6) #?
7) , ?
8) ?
9) ?
10) , System.
11) try/catch - .
12) " ", ?
|
|
13) .
14) throw? .