FomatException ถูกส่งออกมาเมื่อรูปแบบของอาร์กิวเมนต์ไม่ถูกต้อง
เรามาดูตัวอย่างกัน
เมื่อเราตั้งค่าอื่นที่ไม่ใช่ int เป็น int.Parse() วิธีการนั้น FormatException จะถูกส่งออกไปดังที่แสดงด้านล่าง -
ตัวอย่าง
using System;
class Demo {
static void Main() {
string str = "3.5";
int res = int.Parse(str);
}
} ข้อผิดพลาดต่อไปนี้เกิดขึ้นเมื่อคอมไพล์โปรแกรมด้านบนเนื่องจากเราได้ส่งค่าอื่นที่ไม่ใช่จำนวนเต็ม
ผลลัพธ์
Unhandled Exception: System.FormatException: Input string was not in a correct format.