2

I am trying to catch an error on object creation, because this object can and should sometimes throw an error.

try {
    $obj = new MyObject();
} catch (Exception $e) {
    echo 'Caught exception: ';
}

I want to do a lot of things with this new object, but only IF it was created without throwing an exception.

The problem is that I do not wish to do all these things inside the try catch block. How would I accomplish this?

Thanks a lot Michael

2
  • 2
    why do you want to handle this outside the try/catch? that is precisely what it is designed for... Commented Dec 9, 2011 at 12:17
  • Could try return false in the catch block? That way if the object exists, you can keep going and don't have to keep all your code nested inside the try.If the object doesn't exist, the function that calls that code will return false. Commented Dec 9, 2011 at 12:23

3 Answers 3

4

I really can't see any reason for what you are asking, but maybe the best thing is to do all the other stuff in a function that you call from the try/catch block...

function allMyStuff($obj){
  // do some stuff to $obj here
}

try {
  $obj = new MyObject();
  allMyStuff($obj);
} catch (Exception $e) {
  echo 'Caught exception: ';
}

Otherwise, to do literally as you seem to be asking, you could set a switch before the try/catch block to on, and set it to off in the catch block. That way you can test the switch to see whether to execute all your other stuff.

$mySwitch = true;

try {
  $obj = new MyObject();
} catch (Exception $e) {
  echo 'Caught exception: ';
  $mySwitch = false;
}

if($mySwitch){
  // do some stuff here
}
Sign up to request clarification or add additional context in comments.

2 Comments

Which was your preferred method and why? Or... why do you want to avoid the try/catch block in the first place..?
The second option is my preferred method. If it was possible to avoid the try catch, that would have been even better though.
1

There's no point in doing it outside. It also makes more sense to do all of your actions inside the try/catch block to test it for errors.

You should keep it inside the try/catch block as it is exactly what it was designed for.

Comments

0

Slightly odd - but you could either die or redirect...

try {
    $obj = new MyObject();
} catch (Exception $e) {
    die("Caught exception: {$e->getMessage()}");
}

//program continues as it hasn't "died"

or...

try {
    $obj = new MyObject();
} catch (Exception $e) {
    header("Location:/exceptionHandler.php?e=" . rawurlencode(serialize($e)));
    die();
}

//program continues as it's not been redirected or "died"

... though as everyone else has said - it probably still makes more sense to wrap the whole kaboodle in a try ... catch block.

Comments

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.