Differences between revisions 5 and 14 (spanning 9 versions)
Revision 5 as of 2007-08-21 21:00:32
Size: 385
Editor: gtang
Comment:
Revision 14 as of 2022-02-18 00:42:07
Size: 1314
Editor: TunayDurmaz
Comment:
Deletions are marked like this. Additions are marked like this.
Line 1: Line 1:
'''How to use Exception handling in EMAN2?'''
Line 15: Line 16:
 *  * Here is an example on catching the exceptions:
{{{
void foo()
{
    EMData* e1 = new EMData();
    EMData* e2 = new EMData();
    try {
        e1->read_image("test1.mrc");
        e2->read_image("test2.mrc");
        vector<float> v = e1->calc_fourier_shell_correlation(e2);
    }
    catch (_NullPointerException & null_excp) {
        printf("%s\n", null_excp.what());
        // do something
    }
    catch (E2Exception & exception) {
        printf("%s\n", exception.what());
    }
}
}}}
 * Note: NOTE: A XYZ exception is thrown as XYZException, but caught as _XYZException. Be aware of the "_".
 * For the possible Exception types. please refer the EMAN2 code in "[[http://blake.bcm.edu/doxygen/exception_8h.html|exception.h]]". You can add new exception type as a sub class of [[http://blake.bcm.edu/doxygen/classEMAN_1_1E2Exception.html|E2Exception]].

How to use Exception handling in EMAN2?

  • Here is an example on throwing an exception:

vector< float > EMData::calc_fourier_shell_correlation(EMData * with)
{
    if (!with) {
        throw NullPointerException("NULL input image");
    }
    if (!EMUtil::is_same_size(this, with)) {
        LOGERR("images not same size");
        throw ImageFormatException( "images not same size");
    }
//...
}
  • Here is an example on catching the exceptions:

void foo()
{
    EMData* e1 = new EMData();
    EMData* e2 = new EMData();
    try {
        e1->read_image("test1.mrc");
        e2->read_image("test2.mrc");
        vector<float> v = e1->calc_fourier_shell_correlation(e2);
    }
    catch (_NullPointerException & null_excp) {
        printf("%s\n", null_excp.what());
        // do something
    }
    catch (E2Exception & exception) {
        printf("%s\n", exception.what());
    }
}
  • Note: NOTE: A XYZ exception is thrown as XYZException, but caught as _XYZException. Be aware of the "_".
  • For the possible Exception types. please refer the EMAN2 code in "exception.h". You can add new exception type as a sub class of E2Exception.

eman2Exception (last edited 2022-02-18 00:42:07 by TunayDurmaz)