Main menu Toggle menu

Why Exception Handling Is Handy

Why Exception Handling Is Handy

PHP programming languageFor starters, exception handling brings order to the error identification and management process through the use of a generalized strategy for not only identifying and reporting application errors, but also specifying what the program should do once an error is encountered. Furthermore, exception handling syntax promotes the separation of error handlers from the general application logic, resulting in considerably more organized, readable code. Most languages that implement exception handling abstract the process into four steps:

1. The application attempts to perform some task.
2. If the attempt fails, the exception-handling feature throws an exception.
3. The assigned handler catches the exception and performs any necessary tasks.
4. The exception-handling feature cleans up any resources consumed during the attempt.

In a perfect world, your program would run like a well-oiled machine, devoid of both internal and userinitiated errors that disrupt the flow of execution. However, programming, like the real world, often involves unforeseen happenings that disrupt the flow of events. In programmer’s lingo, these unexpected happenings are known as exceptions. Some programming languages have the capability to react gracefully to an exception, a behavior known as exception handling. When an error is detected, the code issues, or throws, an exception. In turn, the error-handling code takes ownership of the exception, or catches it. The advantages to such a strategy are many.

Almost all languages have borrowed from the C++ syntax, known as try/catch. Here’s a simple
pseudocode example:

try {
perform some task
if something goes wrong
throw exception("Something bad happened")
// Catch the thrown exception
} catch(exception) {
output the exception message
}

You can also create multiple handler blocks, which allows you to account for a variety of errors. You can accomplish this either by using various predefined handlers or by extending one of the predefined handlers, essentially creating your own custom handler. PHP currently only offers a single handler, exception. However, that handler can be extended if necessary. It’s likely that additional default handlers will be made available in future releases. For the purposes of illustration, let’s build on the previous pseudocode example, using contrived handler classes to manage I/O and division-related errors:

try {
perform some task
if something goes wrong
throw IOexception("Could not open file.")
if something else goes wrong
throw Numberexception("Division by zero not allowed.")
// Catch IOexception
} catch(IOexception) {
output the IOexception message
}
// Catch Numberexception
} catch(Numberexception) {
output the Numberexception message
}

If you’re new to exceptions, this standardized approach probably seems like a breath of fresh air.  The next section applies these concepts to PHP by introducing and demonstrating the variety of new exception-handling procedures made available in version 5.

Incoming search terms:

Leave a Reply