ในบทช่วยสอนนี้ เราจะพูดถึงโปรแกรมเพื่อปรับแต่งการทำงานสำหรับข้อยกเว้นที่ไม่ถูกตรวจจับใน C++
โดยปกติ บล็อก try-catch จะจัดการข้อยกเว้น แต่มีบางกรณีที่ไม่มีบล็อก catch ที่ตรงกัน และโปรแกรมเพิ่งจะยุติ ฟังก์ชั่น Termin() นี้สามารถแก้ไขได้ตามความต้องการของผู้ใช้
ตัวอย่าง
#include <exception>
#include <iostream>
using namespace std;
//defining custom terminator
void myhandler(){
cout << "Inside new terminate handler\n";
abort();
}
int main(){
set_terminate(myhandler);
try {
cout << "Inside try block\n";
throw 100;
}
catch (char a){
cout << "Inside catch block\n";
}
return 0;
} ผลลัพธ์
Inside try block Inside new terminate handler