Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

TheFriendlyHacker's avatar

"catching" specific PHP exception with ajax...

Let's say I make an AJAX request to my PHP application, and a FooException is thrown, and a 500 error is returned. The response would look something like:

{
message: "Uh oh! Foo went wrong!",
code:  "42S02",
status_code: "500"
}

I want to execute a specific action if the back-end throws a FooException, but not any other type of exception. Is it possible for the front-end to detect the type of exception that was thrown, and react to it accordingly? I don't want to base the action off of the status code, because other exceptions might return the same code (in this case, 500).

0 likes
2 replies
usama.ashraf's avatar

You'll have to write specific handlers that send back specific messages indicating the exception that was thrown.

1 like
phpMick's avatar

Just return an array, once element for success/failure and another for the error.

Please or to participate in this conversation.