The error and exceptions handling is critical in developing any type of application. Understanding the origin point of an error so that appropriate action is taken ensures more quality to the application. It also facilitates the identification of more technical mistakes as problems when connecting to the database or error in a particular class, for example. Legacy systems that possess a try, catch, and finally structure that, implemented efficiently, enable faster maintenance and, therefore, the lifetime of the system increases considerably.

Try, Catch & Finally in PHP

Try, Catch and Finally make up a large structure that is part of the error handling in applications developed in PHP. It enables the treatment of practically exceptions and can then be used in all application screens. The simplicity of the structure is a differential, and will be explained following:

try {

} catch (Exception $e){

} finally{

}

The first block structure (try) will allocate all the code that needs to be validated. If an error is found, the cursor is automatically redirected to the second part of the structure (catch). In the error handling block, the variable “$ e” stores the error message, identified by the system. From there, the whole treatment of error is the developer’s discretion.

Finally, the last block of the frame (finally) will be triggered regardless of the result of the validation of the code. Usually, the statement implemented in this block is used to “clean” variables or other resources that consume memory on the server.

The structure can still rely on different treatments for each type of exception thrown. That’s because PHP provides several bug formats, adapting each to a respective class:

try {

} catch (InvalidArgumentException $e){

catch (RangeException $e){

catch (Exception $e){

} finally{

}

In the example you can see how is the dealings of specific errors. Each error handling class can then trigger a different type of code according to the error returned by the language. Thus, it is possible to treat more effectively the possible errors that can be displayed in the execution of the source code that will be implemented and validated by the structure.

You might also like…

PROCESAMIENTO AJAX EN SCRIPTCASE

Ajax en los formularios de Scriptcase puede usarse para recargar un campo de tipo de select basado e...

IMPORTAR IMAGENES DE LA BASE DE DATOS Y UTILIZAR CLASES DE BOOSTRAP

Como muchas personas ya saben, Bootstrap es una biblioteca de front-end libre y de código abierto p...

TENDENCIAS DE DESARROLLO WEB PARA 2017

Usted verá en este post tendencias para 2017 en Diseño Web, en el medio digital y en el desarrollo...

Comment this post

Get new posts, resources, offers and more each week.

We will use the information you provide to update you about our Newsletter and Special Offers. You can unsubscribe any time you want by clinck in a link in the footer of any email you receive from us, or by contacting us at sales@scriptcase.net. Learn more about our Privacy Police.