Changeset 3730


Ignore:
Timestamp:
Apr 10, 2018, 9:36:08 AM (12 months ago)
Author:
Peter
Message:

new function print_what() to print error message from std::nested_exception

Location:
trunk
Files:
1 added
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/configure.ac

    r3716 r3730  
    118118                AC_DEFINE([YAT_HAVE_LOG2], [1],
    119119                          [Define if std::log2 exists])],
     120               [], [$enable_cxx11])
     121
     122  YAT_CXX_THROW_IF_NESTED([yat_have_throw_if_nested=yes
     123                           AC_DEFINE([YAT_HAVE_THROW_IF_NESTED], [1],
     124                                     [Define if std::throw_if_nested exists])],
    120125               [], [$enable_cxx11])
    121126
  • trunk/m4/yat_cxx11.m4

    r3666 r3730  
    9999
    100100
     101# YAT_CXX_THROW_IF_NESTED([action-if-found], [action-if-not-found],
     102#                         [search-options])
     103#
     104# Test whether $CXX supports std::nested_exception. If not and
     105# search-options is 'yes', try turning on support with a number of
     106# different switches. If a successful switch is found invoke shell
     107# action-if-found; else issue action-if-not-found.
     108AC_DEFUN([YAT_CXX_THROW_IF_NESTED],
     109[
     110YAT_CXX_TRY_CXX11([throw_if_nested], [ dnl header
     111  @%:@include <exception>
     112  void foo(void);
     113  ],[ dnl body
     114  try {
     115    foo();
     116  }
     117  catch (std::runtime_error& e) {
     118    std::throw_with_nested(std::runtime_error("foo"), e);
     119  }
     120  catch (std::exception& e) {
     121    std::rethrow_if_nested(e);
     122  }
     123  ], [$1], [$2], [$3])
     124])
     125
     126
    101127# YAT_CXX_TRY_CXX11(feature, prologue, body, [action-if-success],
    102128#                   [action-if-failed], [search-options])
  • trunk/test/Makefile.am

    r3723 r3730  
    9090  test/phred.test \
    9191  test/pileup.test \
     92  test/print_what.test \
    9293  test/priority_queue.test \
    9394  test/poisson.test \
  • trunk/yat/utility/config_public.h.in

    r3666 r3730  
    9696/// Define if std::log2 exists
    9797#undef YAT_HAVE_LOG2
     98/// Define if std::throw_if_nested exists
     99#undef YAT_HAVE_THROW_IF_NESTED
    98100#endif
    99101
  • trunk/yat/utility/utility.cc

    r3698 r3730  
    185185    mkdir_p(dirname(dir), mode);
    186186    mkdir(dir, mode);
     187  }
     188
     189
     190  void print_what(const std::exception& error, std::ostream& out)
     191  {
     192    // implemented as example at
     193    // http://www.cplusplus.com/reference/exception/rethrow_if_nested/
     194    out << error.what() << "\n";
     195#ifdef YAT_HAVE_THROW_IF_NESTED
     196    try {
     197      std::rethrow_if_nested(error);
     198    }
     199    catch (const std::exception& nested) {
     200      print_what(nested, out);
     201    }
     202#endif
    187203  }
    188204
  • trunk/yat/utility/utility.h

    r3722 r3730  
    88  Copyright (C) 2006 Jari Häkkinen
    99  Copyright (C) 2007, 2008 Jari Häkkinen, Peter Johansson
    10   Copyright (C) 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017 Peter Johansson
     10  Copyright (C) 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018 Peter Johansson
    1111
    1212  This file is part of the yat library, http://dev.thep.lu.se/yat
     
    5454#include <locale>
    5555#include <istream>
     56#include <iostream>
    5657#include <numeric>
    5758#include <string>
     
    347348#define YAT_MOVE_IF_NOEXCEPT(arg) (arg)
    348349#endif
     350
     351  /**
     352     \brief recursively print exception what
     353
     354     Print \c what() of \a error to \a out. If std::nested_exception
     355     was found during configuration of yat (see
     356     YAT_HAVE_THROW_IF_NESTED) and \a error is nested, the exception
     357     that is nested in \a error is passed to print_what().
     358
     359     \note If nested exception is null, function calls terminate (this
     360     behaviour might change in the future).
     361
     362     \since new in yat 0.16
     363   */
     364  void print_what(const std::exception& error, std::ostream& out=std::cerr);
    349365
    350366  /**
Note: See TracChangeset for help on using the changeset viewer.