Exception handling in an application is very important and is available in almost all current programming languages. In the PHP language, the native Exception class handles any errors that may occur at run time in a certain code. With it is possible to identify and deal with contingencies related to a source code implemented incorrectly or database access errors, for example, not allowing any of them to appear without proper treatment to the end user.

Understanding how the Exception class behaves with an application developed in PHP can help in many situations, especially in solving problems related to programming itself. Therefore, following we present a synopsis, in order to be used as a basis for inspection and knowledge:

Exception {

/* Attributes */

protected string $message ;

private string $string ;

protected int $code ;

protected string $file ;

protected int $line ;

private array $trace ;

/* Methods */

public __construct ([ string $message = NULL [, int $code ]] )

final public string getMessage ( void )

final public int getCode ( void )

final public string getFile ( void )

final public string getLine ( void )

final public array getTrace ( void )

final public string getTraceAsString ( void )

public string __toString ( void )

final private string __clone ( void )

}

Attributes

  • Message: The exception message;
  • String: Exception internal name;
  • Code: The exception code;
  • File: The file name where the exception was thrown;
  • Line: The line where the exception was thrown;
  • Trace: Exception stack trace.

Methods

The exception is captured using the structure try catch:

try {

//code
} catch (Exception $e) {
echo  $e->getMessage(), “n”;
}
In the first part of the structure is implemented the source code to be validated, it will be tested and if there is any error in implementing, the Exception class will trigger an error message to be displayed through the method getMessage().

It is possible that, besides using the exception savailable in the language, to use also customized exceptions as can be seen in the example below:

<?php
function inverse($x) {
if (!$x) {
throw new Exception(‘Division by zero.’);
}
return 1/$x;
}

try {
echo inverse(5) . “n”;
echo inverse(0) . “n”;
} catch (Exception $e) {
echo ‘Exception caught: ‘,  $e->getMessage(), “n”;
}

// Continuous execution
echo “Hello worldn”;
?>

In the example, a custom error message is returned, using the class constructor itself. For that exception to be captured and displayed, it is necessary again to use the structure try catch.

Check out more content on our blog!
Learn all about Scriptcase.

By ,

October 21, 2015

a

You might also like…

“We have developed the Connected Citizen Card, which assists public administrators in decision-making and policy creation.”(Assist Soluções em TI)

Check below how Assist Soluções em TI used Scriptcase to modernize and streamline the development...

AI-Driven Software Development: The Role of ChatGPT

In the rapidly evolving world of technology, artificial intelligence (AI) is playing an increasingl...

Low-Code: The Key to Accessible Digital Transformation

Digital transformation is imperative for companies wishing to remain competitive in the current era...

You might also like…

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.