2016-08-29 13 views
8

Bağlamında üzerinde ele

Ben bu basit koduna sahip değildir:İstisna fstream açık

g++ -std=c++11 -g // ... 

derleyici sürümü geçerli:

#include <iostream> 
#include <fstream> 
#include <system_error> 

int main(int argc, char *argv[]) { 
    std::ifstream file; 
    file.exceptions(std::ios::failbit | std::ios::badbit); 

    try { 
    file.open("a_file_does_not_exist.txt", std::ios::in); 
    file.close(); 
    } catch(const std::ios_base::failure& err) { 
    std::cerr << err.code() << std::endl; 
    return -1; 
    } 

    return 0; 
} 

Sadece tamamlamak için, bu derleme komutu g ++ (GCC) 6.1.1.

Platform: arch-linux 4.7.2-1.


sorun

Tahmin edebileceğiniz gibi, dosya yüzden yöntem file.open(...) bir istisna durumu da mevcut değil. Sorun şu ki, kodunu çalıştırdığımda bir istisna işlenmiyor ve std::terminate çağrılıyor.

garip bir şey çıktı şudur: okuyabilir gibi

terminate called after throwing an instance of 'std::ios_base::failure' 
    what(): basic_ios::clear 
Annullato (core dump creato) 

, atma sınıf bir std::ios_base::failure, ama benim yakalamak bu doğru sınıftır.

Soruma sorum: ne var?

+4

[Confirmed] (http://coliru.stacked-crooked.com/a/81a72462c322e96f) –

+0

Muhtemelen ilgili: http://stackoverflow.com/questions/35088800/what-effect-would-lwg2349-have/35089910 –

+4

G ++ 5.4.0 –

cevap

-1

Neden istisna işleme için file.good() ve file.fail() kullanıyorsunuz. Yapmaya çalıştığınız şey için mükemmel çalışırdı.