Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The basic example for try catch is something like this:</p> <pre><code>try { // code that throws an exception } catch(...) { // exception handling } </code></pre> <p>Note that the three dots are perfectly valid for catching all exceptions, though you do not know "what" you catch. This is why you should prefer specifying the type in the parentheses.</p> <p>The exception to be caught can be any type beginning from int and ending with a pointer to an object that derives from a exception class. This concept is very flexible, however you must know what exception may occur. </p> <p>This may be a more concrete example, using std::excpetion: Note the catch by reference.</p> <pre><code>try { throw std::exception(); } catch (const std::exception&amp; e) { // ... } </code></pre> <p>Next example assumes you write C++ with MFC libraries. It clarifies that CException catch is executed because CFileException derives from CException. The CException object deletes itself if it is not needed anymore. Unless your exception derives from CException you should not throw a pointer and stick to the example above.</p> <pre><code>try { throw new CFileException(); } catch (CException* e) { // CFileException is caught } </code></pre> <p>Last but not least this is also important: You may define several catch blocks to catch different exceptions:</p> <pre><code>try { throw new CFileException(); } catch (CMemoryException* e) { // ignore e } catch (CFileException* e) { // rethrow exception so it gets handeled else where throw; } catch (CException* e) { // note that catching the base class should be the last catch } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload