<bdo id='EkPHi'></bdo><ul id='EkPHi'></ul>
<tfoot id='EkPHi'></tfoot>

<small id='EkPHi'></small><noframes id='EkPHi'>

<legend id='EkPHi'><style id='EkPHi'><dir id='EkPHi'><q id='EkPHi'></q></dir></style></legend>
    1. <i id='EkPHi'><tr id='EkPHi'><dt id='EkPHi'><q id='EkPHi'><span id='EkPHi'><b id='EkPHi'><form id='EkPHi'><ins id='EkPHi'></ins><ul id='EkPHi'></ul><sub id='EkPHi'></sub></form><legend id='EkPHi'></legend><bdo id='EkPHi'><pre id='EkPHi'><center id='EkPHi'></center></pre></bdo></b><th id='EkPHi'></th></span></q></dt></tr></i><div id='EkPHi'><tfoot id='EkPHi'></tfoot><dl id='EkPHi'><fieldset id='EkPHi'></fieldset></dl></div>

        为什么 C++ STL iostream 不是“异常友好的"?

        时间:2023-08-02

            <i id='ARziA'><tr id='ARziA'><dt id='ARziA'><q id='ARziA'><span id='ARziA'><b id='ARziA'><form id='ARziA'><ins id='ARziA'></ins><ul id='ARziA'></ul><sub id='ARziA'></sub></form><legend id='ARziA'></legend><bdo id='ARziA'><pre id='ARziA'><center id='ARziA'></center></pre></bdo></b><th id='ARziA'></th></span></q></dt></tr></i><div id='ARziA'><tfoot id='ARziA'></tfoot><dl id='ARziA'><fieldset id='ARziA'></fieldset></dl></div>
            <legend id='ARziA'><style id='ARziA'><dir id='ARziA'><q id='ARziA'></q></dir></style></legend>
                <tbody id='ARziA'></tbody>

              <small id='ARziA'></small><noframes id='ARziA'>

                <bdo id='ARziA'></bdo><ul id='ARziA'></ul>
                  <tfoot id='ARziA'></tfoot>
                  本文介绍了为什么 C++ STL iostream 不是“异常友好的"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我习惯了 Delphi VCL 框架,其中 TStreams 在错误时抛出异常(例如找不到文件、磁盘已满).我正在移植一些代码来使用 C++ STL,并且被 iostreams 发现,默认情况下不会抛出异常,而是设置 badbit/failbit 标志.

                  I'm used to the Delphi VCL Framework, where TStreams throw exceptions on errors (e.g file not found, disk full). I'm porting some code to use C++ STL instead, and have been caught out by iostreams NOT throwing exceptions by default, but setting badbit/failbit flags instead.

                  两个问题...

                  a:为什么会这样 - 对于从一开始就内置异常的语言来说,这似乎是一个奇怪的设计决定?

                  a: Why is this - It seems an odd design decision for a language built with exceptions in it from day one?

                  b:如何最好地避免这种情况?我可以生成像我期望的那样抛出的 shim 类,但这感觉就像重新发明轮子.也许有一个 BOOST 库可以更明智地做到这一点?

                  b: How best to avoid this? I could produce shim classes that throw as I would expect, but this feels like reinventing the wheel. Maybe there's a BOOST library that does this in a saner fashion?

                  推荐答案

                  1. C++ 从一开始就没有例外.C 类"1979年开始,1989年加入异常. 同时,streams库早在1984年就写好了(后来在1989年变成了iostreams(后来在1991年被GNU重新实现)),只是一开始就不能使用异常处理.

                  1. C++ wasn't built with exceptions from day one. "C with classes" started in 1979, and exceptions were added in 1989. Meanwhile, the streams library was written as early as 1984 (later becomes iostreams in 1989 (later reimplemented by GNU in 1991)), it just cannot use exception handling in the beginning.

                  参考:

                  • Bjarne Stroustrup,C++ 的历史:1979−1991
                  • C++ 库

                  可以通过.exceptions 方法.

                  You can enable exceptions with the .exceptions method.

                  // ios::exceptions
                  #include <iostream>
                  #include <fstream>
                  #include <string>
                  
                  int main () {
                      std::ifstream file;
                      file.exceptions(ifstream::failbit | ifstream::badbit);
                      try {
                          file.open ("test.txt");
                          std::string buf;
                          while (std::getline(file, buf))
                              std::cout << "Read> " << buf << "
                  ";
                      }
                      catch (ifstream::failure& e) {
                          std::cout << "Exception opening/reading file
                  ";
                      }
                  }
                  

                  这篇关于为什么 C++ STL iostream 不是“异常友好的"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:从 std::cout 或 std::ofstream(file) 获取 std::ostream 下一篇:在 try/catch 块中新建 (std::nothrow) 与新建

                  相关文章

                  最新文章

                      <bdo id='fv0XL'></bdo><ul id='fv0XL'></ul>
                    <legend id='fv0XL'><style id='fv0XL'><dir id='fv0XL'><q id='fv0XL'></q></dir></style></legend>

                      <tfoot id='fv0XL'></tfoot>
                    1. <small id='fv0XL'></small><noframes id='fv0XL'>

                      <i id='fv0XL'><tr id='fv0XL'><dt id='fv0XL'><q id='fv0XL'><span id='fv0XL'><b id='fv0XL'><form id='fv0XL'><ins id='fv0XL'></ins><ul id='fv0XL'></ul><sub id='fv0XL'></sub></form><legend id='fv0XL'></legend><bdo id='fv0XL'><pre id='fv0XL'><center id='fv0XL'></center></pre></bdo></b><th id='fv0XL'></th></span></q></dt></tr></i><div id='fv0XL'><tfoot id='fv0XL'></tfoot><dl id='fv0XL'><fieldset id='fv0XL'></fieldset></dl></div>