The Exception Class

This lesson discusses how you can use the Exception class for exception handling using various coding scenarios.

We'll cover the following

PHP has a class-based exception handling mechanism. The Exception class is a built-in class with various methods and properties. In this lesson, we will be just be concerned with learning the basics of exception handling using the PHP Exception class.

Components

  • try: It is the block of code in which exception may arise.

  • catch: It is the block of code that will be executed when a particular exception is thrown.

  • throw: It is used to throw an exception. It is also used to list the exceptions that a function throws, but doesn’t handle itself.

  • finally: This is the block of code that executes at the end once the exception is thrown and/or handled.

Run the code below to see how exception handling works:

Create a free account to access the full course.

By signing up, you agree to Educative's Terms of Service and Privacy Policy