Syntax

PHP Try / Catch Syntax

In PHP, try / catch: handles exceptions — try { ... } catch (Exception $e) { echo $e->getMessage(); }

Quick Answer

Syntax
try { ... } catch (Exception $e) { echo $e->getMessage(); }
PHPSyntax

Try / Catch

Code example
try { ... } catch (Exception $e) { echo $e->getMessage(); }

Handles exceptions.

Learn More

How to try / catch in PHP

  1. Open your PHP file or editor.
  2. Copy the example, then replace its sample names and values for your code.

PHP Try / Catch Syntax

Use try { ... } catch (Exception $e) { echo $e->getMessage(); }.

Related Syntax References

Frequently asked questions

What is the PHP syntax for try / catch?

Use this pattern: try { ... } catch (Exception $e) { echo $e->getMessage(); }

Can I copy this PHP code example?

Yes. Use Copy code and adapt the example to your program.

Is this PHP syntax different on Mac or Windows?

No. The language syntax is the same across operating systems.

Official source · How we verify