การจัดการข้อยกเว้นใช้เพื่อจัดการข้อยกเว้น เราสามารถใช้ try catch block เพื่อป้องกันโค้ดได้ สามารถโยนข้อยกเว้นที่ใดก็ได้ภายในบล็อคโค้ด คำหลัก "throw" ใช้เพื่อส่งข้อยกเว้น
นี่คือตัวอย่างการใช้ภาษา C++
ตัวอย่าง
#include <iostream>
using namespace std;
int display(int x, int y) {
if( y == 0 ) {
throw "Division by zero condition!";
}
return (x/y);
}
int main () {
int a = 50;
int b = 0;
int c = 0;
try {
c = display(a, b);
cout << c << endl;
} catch (const char* msg) {
cerr << msg << endl;
}
return 0;
} ผลลัพธ์
นี่คือผลลัพธ์
Division by zero condition!