Miryafa
Miryafa

Reputation: 192

What is the exit code after a php exception?

I'm working with a program that runs a command-line PHP script. The PHP script has been throwing exceptions, but the program has been seeing exit code 0 (and not checking for exceptions).

Upvotes: 2

Views: 4560

Answers (1)

Philipp
Philipp

Reputation: 15629

In case of a exception, the return status code is 255. You could simply test this inside your bash with a simple script.

exc.php

<?php throw new Exception();

and run

php exc.php
echo $? //prints 255

However, you should keep in mind, this is only valid, if you don't define your own exception handler. In case you define your own handler, you have to return the return code manually inside the exception handler.

exc2.php

<?php
set_exception_handler(function() {});
throw new Exception();

returns status code 0. If you want an status code, use exit

<?php
set_exception_handler(function() {
    exit(42);
});
throw new Exception("","","");

now the script returns the status code 42

Upvotes: 9

Related Questions