What is an Exception?An exception is an object that describes an error or unexpected behaviour of a PHP script. Show
Exceptions are thrown by many PHP functions and classes. User defined functions and classes can also throw exceptions. Exceptions are a good way to stop a function when it comes across data that it cannot use. Throwing an ExceptionThe If an exception is not caught, a fatal error will occur with an "Uncaught Exception" message. Lets try to throw an exception without catching it: Example<?php echo divide(5, 0); Try it Yourself » The result will look something like this: Fatal error: Uncaught Exception: Division by zero in C:\webfolder\test.php:4 The try...catch StatementTo avoid the error
from the example above, we can use the Syntax try { ExampleShow a message when an exception is thrown: <?php try { Try it Yourself » The catch block indicates what type of exception should be caught and the name of the variable which can be used to access the exception. In the example above, the type of exception
is The try...catch...finally StatementThe Syntax try { ExampleShow a message when an exception is thrown and then indicate that the process has ended: <?php try { Try it Yourself » ExampleOutput a string even if an exception was not caught: <?php try { Try it Yourself » The Exception ObjectThe Exception Object contains information about the error or unexpected behaviour that the function encountered. Syntaxnew Exception(message, code, previous) Parameter Values
MethodsWhen catching an exception, the following table shows some of the methods that can be used to get information about the exception:
ExampleOutput information about an exception that was thrown: <?php try { Try it Yourself » Complete Exception ReferenceFor a complete reference, go to our Complete PHP Exception Reference. The reference contains descriptions and examples of all Exception methods. How are exceptions handled in PHP?The primary method of handling exceptions in PHP is the try-catch. In a nutshell, the try-catch is a code block that can be used to deal with thrown exceptions without interrupting program execution. In other words, you can "try" to execute a block of code, and "catch" any PHP exceptions that are thrown.
What are exceptions in PHP?An exception is an object that describes an error or unexpected behaviour of a PHP script. Exceptions are thrown by many PHP functions and classes. User defined functions and classes can also throw exceptions. Exceptions are a good way to stop a function when it comes across data that it cannot use.
Does throwing an exception stop execution PHP?When an exception is thrown, code following the statement will not be executed, and PHP will attempt to find the first matching catch block. If an exception is not caught, a PHP Fatal Error will be issued with an " Uncaught Exception ... " message, unless a handler has been defined with set_exception_handler().
Why we use try and catch in PHP?PHP supports using multiple catch blocks within try catch. This allows us to customize our code based on the type of exception that was thrown. This is useful for customizing how you display an error message to a user, or if you should potentially retry something that failed the first time.
|