Feature #1604

Allow to raise GException from cscripts

Added by Mayer Michael over 7 years ago. Updated over 7 years ago.

Status:ClosedStart date:
Priority:NormalDue date:
Assigned To:Mayer Michael% Done:


Target version:-


I realised that the following code doesn’t work:

import gammalib
raise gammalib.GException.invalid_argument("","")

The error message is:
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'GException'

It seems the class GException is not available from python. Nevertheless, we use this exception call quite frequently in cscripts. This hasn’t been a problem so far. We should however implement this properly but I am not entirely sure how to do that. Any ideas?


No recurrence.


#1 Updated by Mayer Michael over 7 years ago

Quick workaround for the moment could be to change

raise gammalib.GException(origin,msg)

raise RuntimeError(msg)

#2 Updated by Knödlseder Jürgen over 7 years ago

Indeed, I never checked this properly.

C++ exceptions are in fact translated into Python exceptions using the following SWIG declaration that is added to all the GammaLib wrapper scripts:

%exception {
    try {
    catch (const GException::out_of_range& e) {
        SWIG_exception(SWIG_IndexError, e.what());
    catch (const GExceptionHandler& e) {
        SWIG_exception(SWIG_RuntimeError, e.what());
    catch (const std::exception& e) {
        SWIG_exception(SWIG_RuntimeError, e.what());

This means that basically all exceptions show up as Python run time errors. I would therefore support using
raise RuntimeError(msg)
within the cscripts.

#3 Updated by Mayer Michael over 7 years ago

  • Status changed from New to Pull request
  • Assigned To set to Mayer Michael

I have made the changes on branch 1604-Gexceptions-in-cscripts.

#4 Updated by Knödlseder Jürgen over 7 years ago

  • Status changed from Pull request to Closed

Merged into devel

Also available in: Atom PDF