You get a bonus - 1 coin for daily activity. Now you have 1 coin

How to create custom exceptions - Try-catch exception handling: what's

Lecture



Это продолжение увлекательной статьи про try-catch.

...

normal !important; font-variant-east-asian: normal !important; font-stretch: normal !important; line-height: normal !important; margin: 0px !important; outline: 0px !important; overflow: visible !important; padding: 0px 0.5em 0px 1em !important; position: static !important; text-align: right !important; vertical-align: baseline !important; width: auto !important; min-height: auto !important; white-space: pre !important;">13

14
15
16
17
try {
// init bootstrapping phase
$config_file_path = "config.php";
if (!file_exists($config_file_path))
{
throw new Exception("Configuration file not found.");
}
// continue execution of the bootstrapping phase
} catch (Exception $e) {
echo $e->getMessage();
die();
}
?>

As you can see in the example above, we check for the presence of the config.php file at the start of the bootstrap phase. If the config.php file is found, execution continues normally. On the other hand, we'll throw an exception if the config.php file doesn't exist. In addition, we'd like to stop execution if there is an exception!

This is how you can use exceptions in your applications. You should throw exceptions for exceptional use cases — you shouldn't needlessly throw exceptions for errors such as invalid user credentials, incorrect directory access permissions, and so on, which you often expect. Those are better handled with generic error messages in the normal application execution flow.

So that was an example of exception handling using the default Exception class. In the next section we'll look at how you can extend the base Exception class and create your own custom exceptions in your application.

How to create custom exceptions

In this section we'll discuss how you can create custom exceptions in your applications. In fact, we'll extend the example we just discussed in the previous section to demonstrate custom exceptions.

In the previous example, we threw a configuration exception using the default Exception class. That's fine if you just want to deal with the error message. However, sometimes you want to do a bit more depending on the type of exception being thrown. That's why custom exceptions are useful.

Let's revisit the previous example, as shown in the following snippet.

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class ConfigFileNotFoundException extends Exception {}

продолжение следует...

Продолжение:


Часть 1 Try-catch exception handling: what's the difference between exceptions and errors?
Часть 2 How to create custom exceptions - Try-catch exception handling: what's
Часть 3 The Finally block - Try-catch exception handling: what's the difference
Часть 4 The difference between bugs, exceptions, and end-user errors - Try-catch

See also

Comments

To leave a comment

If you have any suggestion, idea, thanks or comment, feel free to write. We really value feedback and are glad to hear your opinion.
To reply

Lectures and tutorial on "Running server side scripts using PHP as an example (LAMP)"

Terms: Running server side scripts using PHP as an example (LAMP)