1. <legend id='1D8vo'><style id='1D8vo'><dir id='1D8vo'><q id='1D8vo'></q></dir></style></legend>
    • <bdo id='1D8vo'></bdo><ul id='1D8vo'></ul>

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

    <small id='1D8vo'></small><noframes id='1D8vo'>

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

      Why are C++ STL iostreams not quot;exception friendlyquot;?(为什么 C++ STL iostreams 不是“异常友好的?)
      1. <tfoot id='gk68Q'></tfoot>

        <legend id='gk68Q'><style id='gk68Q'><dir id='gk68Q'><q id='gk68Q'></q></dir></style></legend>

        1. <small id='gk68Q'></small><noframes id='gk68Q'>

          • <i id='gk68Q'><tr id='gk68Q'><dt id='gk68Q'><q id='gk68Q'><span id='gk68Q'><b id='gk68Q'><form id='gk68Q'><ins id='gk68Q'></ins><ul id='gk68Q'></ul><sub id='gk68Q'></sub></form><legend id='gk68Q'></legend><bdo id='gk68Q'><pre id='gk68Q'><center id='gk68Q'></center></pre></bdo></b><th id='gk68Q'></th></span></q></dt></tr></i><div id='gk68Q'><tfoot id='gk68Q'></tfoot><dl id='gk68Q'><fieldset id='gk68Q'></fieldset></dl></div>
              <tbody id='gk68Q'></tbody>
              <bdo id='gk68Q'></bdo><ul id='gk68Q'></ul>
              • 本文介绍了为什么 C++ STL iostreams 不是“异常友好的"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

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

                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++ 的历史:19791991 年
                • 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 iostreams 不是“异常友好的"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                Unable to access non-const member functions of objects in C++ std::set(无法访问 C++ std::set 中对象的非常量成员函数)
                How should a size-limited stl-like container be implemented?(应该如何实现大小受限的 stl 类容器?)
                Constructing std::function argument from lambda(从 lambda 构造 std::function 参数)
                STL BigInt class implementation(STL BigInt 类实现)
                Sync is unreliable using std::atomic and std::condition_variable(使用 std::atomic 和 std::condition_variable 同步不可靠)
                Move list element to the end in STL(在 STL 中将列表元素移动到末尾)

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

                            <tbody id='qivL5'></tbody>
                          <tfoot id='qivL5'></tfoot>

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