Tải bản đầy đủ (.pptx) (15 trang)

Xử lí ngoại lệ trong C#

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (69.74 KB, 15 trang )


Exception Handling



 !"#!"$%&%'()*+,-./012+*'
(30)042*2-

5#+6#72089#:%!(;"+6#72!)%<=)>/0
+?'$*)+@A$B+6#7!2*4%7,

C)DE";A00%&'+6+@F1@A!
!%&.2=C2)A0)"2A+G"
H2%&*+,
I

H?J2*+,/0*0<(KL2#M7@(0%(K#A+,2*'""
32

C42%&?"3"(K/4*4N?4%&?N?"3"(K
/8C5O3*.P0@+G"(KL#M2*11

O"J*2-*+,4%4*'3)2*+,/0
)BQ0%;
32R

J@S0&2

J$/J'Q0@S02

J%;?'A02



J%;32%?A0
T@S0&
2

TJ@%4M0M)0((5?(UR

VA(85F0((2W)N0((2*%4M0<4 )4M0((A

VA(8VA(5F#7)2*+,(0602AQ08C5O#:F
V0%XYZ5A0%1@Q0V0%*BA=

VA(8[05F#7)2*+,*LP0\)-S0
]1@0A
#7

]#[((5FA!*4)$%&*+,/A!

^#A_5F0`

a05F%&**L#2

b#XXcd05F+,)e(;Q000Af(=

Cdc5F04%&,

[C5F*;(;A

b0#0(5F/)%&,


[5F0(;%N
)1Q0@
5W)

gT%h(iFjdT#D*4J! JZ(S0&*BA*Q2

^00hb^0AiF!,) %Af0#!$*LP0 *L1A

gd(hiF-0Q02

b5h5iF42A*+,/M$1%;0?S0*<*;+,
23*2-%;0

]((0h(iF&2

Vh(iF$S#: *;+,A0

V0%O0h(iF#k+GSN(0%

O0Vh]#0(iF*;+,&+GSA02
C/2@M%?0
Z
public class Humans
{
public int Age
{
get { return Age; }
set
{
if (value >= 0)

{
Age = value;
Console.WriteLine(“Tuoi la {0}”, Age);
}
else throw new Exception(“Tuoi khong chinh xac”);
}
}
}
H1*;+,A0*;+,A?'
/*2#N?@M%?0Z)
*+,/0J*;+,S0&2
I2@%;
Al0
static void Main(string[] args)
{
Console.WriteLine(“Simple Exception Example”);
Humans a = new Humans() { Age = 30; }
try
{
for (int i = 0; i < 4; i++)
a.Age -= 10;
}
catch (Exception e)
{
Console.WriteLine(“Exception Message: {0}”, e.Message);
}
}
m'I20(3#:%;Al08H;
A#7*'S0*2?%n/02
8H;0(KI2*'3

I2@%;
Al0

o#:$0WQ00;pBMqr;srJrrlJr"A0(Q0+GS[
(K/0J28V0*?%;0(KI2A0"&)
Kết quả:
Tuoi la 30
Tuoi la 20
Tuoi la 10
Tuoi la 0
Exception Message: Tuoi khong chinh xac
m'I20(3#:%;Al08H;
A#7*'S0*2?%n/02
8H;0(KI2*'3
I2@%;
0
while (true)
{
try
{
Console.Write(“Nhap 1 so tu 1 den 10, nhap rong de thoat: ”);
string input = Console.ReadLine();
if (input == “”) break;
int index = Convert.ToInt32(input);
if (index < 1 || index > 10)
throw new IndexOutOfRangeException(“Ban da nhap so ” + input);
Console.WriteLine(“So ban nhap la {0}”, index);
}
catch(IndexOutOfRangeException e)
{

Console.WriteLine(“Co loi! Ban phai nhap so tu 1 den 10! {0}”, e.Message);
}
catch(Exception e)
{
Console.WriteLine(“Co loi! {0}”, e.Message);
}
}
I2@%;
0

Khi chạy:
Nhap 1 so tu 1 den 10, nhap rong de thoat: 5
So ban nhap la 5
Nhap 1 so tu 1 den 10, nhap rong de thoat: 20
Co loi! Ban phai nhap so tu 1 den 10! Ban da nhap so 20
Nhap 1 so tu 1 den 10, nhap rong de thoat: abc
Co loi! Input string was not in a correct format.
Nhap 1 so tu 1 den 10, nhap rong de thoat:
H;0%&?0
(;

H;0%&?0(;(KI*+,N2A0Q0t2%&*+,4R 
%&*+,R89#:uuN4*?'*+,/0+J240N*2-
Auu?(KI*+,)2%&#DM0((VA(85
catch
{
Console.WriteLine(“Co loi nao do xay ra”);
}
H;v0A


C%0B<J#7? %&A0289#:F%0\Jv0
)$v*?#7? %&A02"0D*?v2

]1)A4*+0!*?v%;A%;08OA$)AA0(<7
 

R%;v0A*'<0)A
9#:%;
Al0lv0A
string path = @"c:\users\public\test.txt";
var file = new System.IO.StreamReader(path);
char[] buffer = new char[10];
try
{
file.ReadBlock(buffer, index, buffer.Length);
}
catch (System.IO.IOException e)
{
Console.WriteLine("Error reading from {0}. Message = {1}", path, e.Message);
}
finally
{
if (file != null)
{
file.Close();
}
}

Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Tải bản đầy đủ ngay
×