Exception Hierarchy and Built-in Exception Classes
Sveip for å vise menyen
To understand error handling in PHP, you need to know how exceptions are structured and which built-in classes are available for use. At the top of the PHP exception hierarchy sits the Exception class. This class is the ancestor for most other exception types in PHP and provides the basic functionality for exception objects, such as storing an error message, code, and stack trace.
Many other exception classes extend from Exception. Some of these built-in descendants are designed for specific error situations, making your code more expressive and robust. For example, RuntimeException is used for errors that occur during program execution, while InvalidArgumentException signals that a function received an argument that is not valid. By using these specific classes, you can write code that reacts differently to various error types and improves debugging.
BuiltInExceptions.php
12345678910111213141516171819202122<?php // Base exception class class Exception {} // Thrown when an error occurs during runtime class RuntimeException extends Exception {} // Thrown when a function receives an invalid argument class InvalidArgumentException extends Exception {} // Typical use cases: function divide($a, $b) { if (!is_numeric($a) || !is_numeric($b)) { throw new InvalidArgumentException("Arguments must be numbers"); } if ($b == 0) { throw new RuntimeException("Division by zero"); } return $a / $b; }
You should choose a specific built-in exception class based on the kind of error you want to signal. In the code sample above, InvalidArgumentException is used when the input arguments are not numbers, making it clear that the error is related to invalid input. RuntimeException is used for division by zero, which is a problem that occurs during the program's execution, not because of invalid arguments. By selecting the most appropriate exception class, you help other developers (and yourself) quickly understand what went wrong and where to look for the problem.
CatchingSpecificException.php
123456789<?php try { divide("apple", 5); } catch (InvalidArgumentException $e) { echo "Input error: " . $e->getMessage(); } catch (Exception $e) { echo "General error: " . $e->getMessage(); }
Catching a specific exception type, as shown above, allows you to handle different error conditions in a targeted way. For instance, if you catch an InvalidArgumentException, you might prompt the user to correct their input, while a generic Exception catch block could be used for logging or displaying a general error message. This approach leads to more precise and maintainable error handling, because each type of error can be handled in the most appropriate way for your application's needs.
Definition: An InvalidArgumentException is a built-in PHP exception class that signals a function or method received an argument of the wrong type or value. Use it when you want to clearly indicate that the caller has provided data that does not meet your function's requirements.
1. Which class is the parent of all exceptions in PHP?
2. Why might you want to catch a specific exception type instead of the generic Exception?
Takk for tilbakemeldingene dine!
Spør AI
Spør AI
Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår