exceptioneiffeldesign-by-contract

rescue how to raise further or forget an exception


How do I raise an exception further in eiffel? I have 3 cases

  1. I want to retry

    a_feature
        local
            l_retries_count: INTEGER
        do
            some_potential_failing_feature
        rescue
            if l_retries_count <= 3 then
                l_retries_count := l_retries_count + 1
                retry
            end
        end
    
  2. I want to do close db connection and ignore exception

    a_feature
        do
            some_potential_failing_feature
        rescue
            db_connection.close
        end
    
  3. I want to do close db connection and send an email to an admin and ignore

    a_feature
        do
            some_potential_failing_feature
        rescue
            db_connection.close
            send_email_to_admin
        end
    
  4. I want to close db_connection and raise the exception further, I'll put all the cases I can think about into the above code

    a_feature
        local
            l_retries_count: INTEGER
        do
            some_potential_failing_feature
        rescue
            if l_retries_count <= 3 then
                l_retries_count := l_retries_count + 1
                log_error ("Error, retrying for " + l_retries_count.out + "th time")
                retry
            else
                db_connection.close
                send_email_to_admin
                -- raise the_created_exception_from_some_potential_failing_feature -- how do I do that?
            end
        end
    

Solution

  • You can try one of the following:

    {EXCEPTION_MANAGER}.last_exception.original.raise
    {EXCEPTION_MANAGER}.last_exception.cause.raise
    {EXCEPTION_MANAGER}.last_exception.raise
    

    The first one goes through the chain of exceptions ignoring those triggered as a result of failed routines. It might be the one you are looking for.

    The next two retrieve either the cause of the current exception or the current exception itself, though it might be not the one you are looking for because it depends on the context of the exception and the nested calls.