From mboxrd@z Thu Jan 1 00:00:00 1970 From: Avi Kivity Subject: [PATCH kvm-unit-tests v2 04/14] Add try_main() for running a program under an exception handler Date: Wed, 15 Dec 2010 18:09:33 +0200 Message-ID: <1292429383-15326-5-git-send-email-avi@redhat.com> References: <1292429383-15326-1-git-send-email-avi@redhat.com> To: Marcelo Tosatti , kvm@vger.kernel.org Return-path: Received: from mx1.redhat.com ([209.132.183.28]:50512 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750874Ab0LOQKH (ORCPT ); Wed, 15 Dec 2010 11:10:07 -0500 Received: from int-mx01.intmail.prod.int.phx2.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) by mx1.redhat.com (8.13.8/8.13.8) with ESMTP id oBFGA6E4001878 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Wed, 15 Dec 2010 11:10:06 -0500 Received: from cleopatra.tlv.redhat.com (cleopatra.tlv.redhat.com [10.35.255.11]) by int-mx01.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id oBFGA4HA015655 for ; Wed, 15 Dec 2010 11:10:06 -0500 In-Reply-To: <1292429383-15326-1-git-send-email-avi@redhat.com> Sender: kvm-owner@vger.kernel.org List-ID: Signed-off-by: Avi Kivity --- api/exception.cc | 13 +++++++++++++ api/exception.hh | 3 +++ 2 files changed, 16 insertions(+), 0 deletions(-) diff --git a/api/exception.cc b/api/exception.cc index 500569a..910bdff 100644 --- a/api/exception.cc +++ b/api/exception.cc @@ -18,3 +18,16 @@ const char *errno_exception::what() std::strerror(_errno), _errno); return _buf; } + +int try_main(int (*main)(int argc, char** argv), int argc, char** argv, + int ret_on_exception) +{ + try { + return main(argc, argv); + } catch (std::exception& e) { + std::fprintf(stderr, "exception: %s\n", e.what()); + } catch (...) { + std::fprintf(stderr, "unknown exception\n"); + } + return ret_on_exception; +} diff --git a/api/exception.hh b/api/exception.hh index 4672760..f78d9a1 100644 --- a/api/exception.hh +++ b/api/exception.hh @@ -13,4 +13,7 @@ private: char _buf[1000]; }; +int try_main(int (*main)(int argc, char** argv), int argc, char** argv, + int ret_on_exception = 127); + #endif -- 1.7.1