Concept
How do you catch exceptions in PHP?
ComputerScienceOne / Catching Exceptions
"To catch an exception in PHP you can use the standard try-catch control block. Optionally (and as of PHP version 5.5.0) you can use the finally block to clean up any resources or execute code regardless of whether or not an exception was raised. Consider the simple task of reading input from a user and manually parsing its value into an integer. If the user enters a non-numeric value, parsing will fail and we should instead throw an exception. Consider the following function.\n\n1 function readNumber() {\n\n2 $input = readline(\"Please enter a number: \");\n\n3 if( is_numeric($input) ) {\n\n4 $value = floatval($input);\n\n5 } else {\n\n6 throw new Exception(\"Invalid input!\");\n\n7 }\n\n8 }"
Related Ideas
- How does PHP support throwing and defining exceptions?ComputerScienceOne · Throwing Exceptions
- How do you catch and handle an exception in Java?ComputerScienceOne · Catching Exceptions
- How can you create and handle custom exception classes in PHP?ComputerScienceOne · Creating Custom Exceptions
- Why should the most specific exception types be caught before general ones?ComputerScienceOne · Throwing Exceptions
- How can Java handle different exception types with multiple catch blocks?ComputerScienceOne · Catching Exceptions
- Why should the most specific exception types be caught before the most general types?ComputerScienceOne · Creating Custom Exceptions
- How can you manually throw an exception in Java?ComputerScienceOne · Creating Custom Exceptions
- What is a checked exception in Java?ComputerScienceOne · Checked Exceptions