OverflowException ถูกส่งออกไปเมื่อค่าพารามิเตอร์อยู่นอกช่วงจำนวนเต็ม
เรามาดูตัวอย่างกัน
เมื่อเราตั้งค่าเป็น int.Parse() วิธีที่อยู่นอกช่วงจำนวนเต็ม OverflowException จะถูกส่งออกไปดังที่แสดงด้านล่าง -
ตัวอย่าง
using System;
class Demo {
static void Main() {
string str = "757657657657657";
int res = int.Parse(str);
}
} ผลลัพธ์
ข้อผิดพลาดต่อไปนี้เกิดขึ้นเมื่อคอมไพล์โปรแกรมด้านบนเนื่องจากเราได้ส่งค่าที่อยู่นอกช่วงจำนวนเต็ม (Int32)
Unhandled Exception: System.OverflowException: Value was either too large or too small for an Int32.