3.10 İstisnalar (Exceptions)
#include <iostream>
#include <stdexcept> // std::runtime_error için
using namespace std;
double bolme(double a, double b) {
if (b == 0) {
throw runtime_error("Bölme hatası: Bir sayı sıfıra bölünemez."); // İstisna fırlatma
}
return a / b;
}
int main() {
double sayi1, sayi2;
cout << "Bir sayı girin: ";
cin >> sayi1;
cout << "Bölmek için bir sayı girin: ";
cin >> sayi2;
try {
double sonuc = bolme(sayi1, sayi2); // try bloğunda işlem
cout << "Sonuç: " << sonuc << endl;
} catch (const runtime_error& e) { // catch bloğunda hata yönetimi
cout << "Hata: " << e.what() << endl; // Hata mesajını yazdır
}
return 0;
}Sonuç
Last updated