3

My script_a.php:

try {
    Class1::tryThis();
}
catch (Exception $e) {
    // Do stuff here to show the user an error occurred
}

Class1::tryThis() has something like:

public function tryThis() {
    Class2::tryThat();
    self::logSuccessfulEvent();
}

The problem is that Class2::tryThat() can throw an exception.

If it does throw an exception, it seems that the line self::logSuccessfulEvent(); still gets executed.

How can I refactor this code so that self::logSuccessfulEvent() only occurs when an exception is not thrown, yet at the same time letting script_a.php know when an exception has been thrown?

3
  • the problem is somewhere else Commented Mar 13, 2012 at 16:33
  • "it seems that"? If tryThat throws an unhandled exception, the next line in tryThis (self::logSuccessfulEvent()) will not be run. Commented Mar 13, 2012 at 16:36
  • @webbiedave and OP: You are absolutely correct. The next line could not run, and it was being executed beforehand where I did not realize. Thus, PHP exceptions work as I expected: no code is executed after the statement that throws the exception in the current block of code. Commented Mar 20, 2012 at 11:23

2 Answers 2

7

This function will return whether or not the operation was successful (true = success, false = failure)

public function tryThis() {
   $success = true;

   try {
       Class2::tryThat();
       self::logSuccessfulEvent();
   } catch( Exception $e) {
       $success = false;
   }

   return $success;
}
Sign up to request clarification or add additional context in comments.

Comments

2

What you're describing does not seem to be the case.

Code:

<?php
class Class1 {
    public function tryThis() {
        echo "Class1::tryThis() was called.\n";
        Class2::tryThat();
        self::logSuccessfulEvent();
    }

    public function logSuccessfulEvent() {
        echo "Class1::logSuccessfulEvent() was called.\n";
    }
}

class Class2 {
    public function tryThat() {
        echo "Class2::tryThat() was called.\n";
        throw new Exception('Exception generated in Class2::tryThat()');
    }
}

try {
    Class1::tryThis();
} catch (Exception $e) {
    echo $e->getMessage(), "\n";
}

Output:

Class1::tryThis() was called.
Class2::tryThat() was called.
Exception generated in Class2::tryThat()

As you can see, the Class1::logSuccessfulEvent() method is never executed when an exception is generated in Class2::tryThat(), and it shouldn't (won't) either. Exceptions bubble up until they are caught or produce a fatal error. Once an exception is caught, control of the program returns to the code after the catch block. In this particular case, that would mean that control of the program never reaches the logging method.

1 Comment

That's how I assumed exceptions would work. Perhaps it's a sign that my code is calling a method before the exception being raised.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.