try { // Some PDO code that may throw an exception } catch (PDOException $e) { $errorCode = $e->getCode(); // Do something with the error code }
try { // Some SoapClient code that may throw a SoapFault } catch (SoapFault $e) { $errorCode = $e->getCode(); // Do something with the error code }
class CustomException extends Exception { public function __construct($message, $code) { parent::__construct($message, $code); } } try { // Some code that may throw a CustomException } catch (CustomException $e) { $errorCode = $e->getCode(); // Do something with the error code }In this example, we define a custom exception class that extends the built-in Exception class. We then catch any CustomException that may occur and retrieve its error code using the getCode method. Package library: This example does not use any package/library.