Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C#

Try/Catch/Finally/Throw Keywords ใน C#

Exception Handling ใน C# ใช้ Keywords 4 ตัว:

  • try - บล็อกโค้ดที่อาจเกิด Exception ตามด้วย Catch Blocks
  • catch - จับ Exception ที่เกิดขึ้น จัดการ Error
  • finally - บล็อกที่รันเสมอ ไม่ว่าจะมี Exception หรือไม่ (เช่น ปิดไฟล์)
  • throw - โยน Exception เมื่อพบปัญหา

Syntax

try
{
    // statements causing exception
}
catch (ExceptionName e1)
{
    // error handling code
}
catch (ExceptionName e2)
{
    // error handling code
}
finally
{
    // statements to be executed
}