Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > de.comp.lang.iso-c++ > #1976

Re: Exception mit Durchfall?

From Stefan Reuther <stefan.news@arcor.de>
Newsgroups de.comp.lang.iso-c++
Subject Re: Exception mit Durchfall?
Date 2016-11-28 17:49 +0100
Organization A noiseless patient Spider
Message-ID <o1hqne.44s.1@stefan.msgid.phost.de> (permalink)
References (2 earlier) <o14123$9bf$1@news2.open-news-network.org> <o17e8r.4ng.1@stefan.msgid.phost.de> <o18o8d$4c6$1@news2.open-news-network.org> <o19ulo.5ek.1@stefan.msgid.phost.de> <o1glul$m01$1@news2.open-news-network.org>

Show all headers | View raw


Am 28.11.2016 um 08:21 schrieb Edzard Egberts:
> Sorry, war also die falsche Gruppe. Allerdings war mir die Sache mit dem
> Destruktor vorher nicht so bewusst, noch einmal ganz langsam für mich:
> Wenn während der Bearbeitung ("Abwicklung") einer Exception eine weitere
> Exception auftritt, kann die nicht mehr abgefangen werden und beendet
> das Programm. Das kann ich mir so merken?

So wie du es formuliert hast, stimmt es nicht, aber vielleicht meinst du
das richtige. Als Merksatz vielleicht formuliert: "es dürfen niemals
zwei Exceptions gleichzeitig unterwegs sein." Und als noch einfacheren
Merksatz: "sorge dafür, dass deine Destruktoren niemals direkt oder
indirekt Exceptions werfen".

Beispiel:
   #include <iostream>
   void foo()
     { throw "ho"; }
   class X {
   public:
     ~X() { foo(); }
   };
   int main() {
     try  {
       X x;
       throw "hi";
     }
     catch (const char* p) {
       std::cout << p << "\n";
     }
   }
Mit der Zeile 'throw "hi"' wirfst du eine Exception. Auf dem Stack liegt
noch das 'X'-Objekt, also wird die Exception kurz gefangen und der
Destruktor aufgerufen. Der Destruktor wirft dummerweise eine Exception.
Wenn der Compiler nun die Exception weiterwerfen will, hat er auf einmal
zwei davon ("hi" und "ho") --> boom.

Ändern wir den Destruktor '~X' in
    ~X() {
      try { foo(); }
      catch (...) { }
    }
wird die "ho"-Exception durch den catch-Block final "behandelt" und nach
dem Verlassen des Destruktors ist wieder nur eine Exception unterwegs.

Aber "während der Bearbeitung der Exception" war auch mal kurz eine
zweite Exception aktiv.

Alternativ vielleicht hier schauen:
http://stackoverflow.com/questions/3318887/what-happens-when-an-exception-is-thrown-while-unwinding-the-stack-from-another


  Stefan

Back to de.comp.lang.iso-c++ | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

Exception mit Durchfall? Edzard Egberts <news@edzeg.net> - 2016-11-18 14:23 +0100
  Re: Exception mit Durchfall? Stefan Reuther <stefan.news@arcor.de> - 2016-11-20 11:18 +0100
    Re: Exception mit Durchfall? Edzard Egberts <news@edzeg.net> - 2016-11-23 13:11 +0100
      Re: Exception mit Durchfall? Stefan Reuther <stefan.news@arcor.de> - 2016-11-24 19:15 +0100
        Re: Exception mit Durchfall? Edzard Egberts <news@edzeg.net> - 2016-11-25 08:11 +0100
          Re: Exception mit Durchfall? Stefan Reuther <stefan.news@arcor.de> - 2016-11-25 18:07 +0100
            Re: Exception mit Durchfall? Edzard Egberts <news@edzeg.net> - 2016-11-28 08:21 +0100
              Re: Exception mit Durchfall? Stefan Reuther <stefan.news@arcor.de> - 2016-11-28 17:49 +0100

csiph-web