コード例 #1
0
ファイル: exception.c プロジェクト: 0x0B501E7E/pjproject
static int test(void)
{
    int rc = 0;
    PJ_USE_EXCEPTION;

    /*
     * No exception situation.
     */
    PJ_TRY {
        PJ_UNUSED_ARG(rc);
    }
    PJ_CATCH_ANY {
        rc = -3;
    }
    PJ_END;

    if (rc != 0)
	return rc;


    /*
     * Basic TRY/CATCH
     */ 
    PJ_TRY {
	rc = throw_id_1();

	// should not reach here.
	rc = -10;
    }
    PJ_CATCH_ANY {
        int id = PJ_GET_EXCEPTION();
	if (id != ID_1) {
	    PJ_LOG(3,("", "...error: got unexpected exception %d (%s)", 
		      id, pj_exception_id_name(id)));
	    if (!rc) rc = -20;
	}
    }
    PJ_END;

    if (rc != 0)
	return rc;

    /*
     * Multiple exceptions handlers
     */
    PJ_TRY {
	rc = throw_id_2();
	// should not reach here.
	rc = -25;
    }
    PJ_CATCH_ANY {
	switch (PJ_GET_EXCEPTION()) {
	case ID_1:
	    if (!rc) rc = -30; break;
	case ID_2:
	    if (!rc) rc = 0; break;
	default:
	    if (!rc) rc = -40;
	    break;
	}
    }
    PJ_END;

    if (rc != 0)
	return rc;

    /*
     * Test default handler.
     */
    PJ_TRY {
	rc = throw_id_1();
	// should not reach here
	rc = -50;
    }
    PJ_CATCH_ANY {
	switch (PJ_GET_EXCEPTION()) {
	case ID_1:
	    if (!rc) rc = 0;
	    break;
	default:
	    if (!rc) rc = -60;
	    break;
	}
    }
    PJ_END;

    if (rc != 0)
	return rc;

    /*
     * Nested handlers
     */
    PJ_TRY {
	rc = try_catch_test();
    }
    PJ_CATCH_ANY {
	rc = -70;
    }
    PJ_END;

    if (rc != 0)
	return rc;

    /*
     * Throwing exception inside handler
     */
    rc = -80;
    PJ_TRY {
	int rc2;
	rc2 = throw_in_handler();
	if (rc2)
	    rc = rc2;
    }
    PJ_CATCH_ANY {
	if (PJ_GET_EXCEPTION() == ID_2) {
	    rc = 0;
	} else {
	    rc = -90;
	}
    }
    PJ_END;

    if (rc != 0)
	return rc;


    /*
     * Return from handler. Returning from the function inside a handler
     * should be okay (though returning from the function inside the
     * PJ_TRY block IS NOT OKAY!!). We want to test to see if handler
     * is cleaned up properly, but not sure how to do this.
     */
    PJ_TRY {
	int rc2;
	rc2 = return_in_handler();
	if (rc2)
	    rc = rc2;
    }
    PJ_CATCH_ANY {
	rc = -100;
    }
    PJ_END;


    return 0;
}
コード例 #2
0
ファイル: exception.c プロジェクト: svn2github/pjproject
static int test(void)
{
    int rc = 0;
    PJ_USE_EXCEPTION;

    /*
     * No exception situation.
     */
    PJ_TRY {
        rc = rc;
    }
    PJ_CATCH_ANY {
        rc = -3;
    }
    PJ_END;

    if (rc != 0)
	return rc;


    /*
     * Basic TRY/CATCH
     */ 
    PJ_TRY {
	rc = throw_id_1();

	// should not reach here.
	rc = -10;
    }
    PJ_CATCH_ANY {
        int id = PJ_GET_EXCEPTION();
	if (id != ID_1) {
	    PJ_LOG(3,("", "...error: got unexpected exception %d (%s)", 
		      id, pj_exception_id_name(id)));
	    if (!rc) rc = -20;
	}
    }
    PJ_END;

    if (rc != 0)
	return rc;

    /*
     * Multiple exceptions handlers
     */
    PJ_TRY {
	rc = throw_id_2();
	// should not reach here.
	rc = -25;
    }
    PJ_CATCH_ANY {
	switch (PJ_GET_EXCEPTION()) {
	case ID_1:
	    if (!rc) rc = -30; break;
	case ID_2:
	    if (!rc) rc = 0; break;
	default:
	    if (!rc) rc = -40;
	    break;
	}
    }
    PJ_END;

    if (rc != 0)
	return rc;

    /*
     * Test default handler.
     */
    PJ_TRY {
	rc = throw_id_1();
	// should not reach here
	rc = -50;
    }
    PJ_CATCH_ANY {
	switch (PJ_GET_EXCEPTION()) {
	case ID_1:
	    if (!rc) rc = 0;
	    break;
	default:
	    if (!rc) rc = -60;
	    break;
	}
    }
    PJ_END;

    if (rc != 0)
	return rc;

    return 0;
}