Title: How to pass error code from wp_error() to PHP exception class?
Last modified: April 26, 2019

---

# How to pass error code from wp_error() to PHP exception class?

 *  Resolved [xootixsupport](https://wordpress.org/support/users/xootixsupport/)
 * (@xootixsupport)
 * [7 years, 1 month ago](https://wordpress.org/support/topic/how-to-pass-error-code-from-wp_error-to-php-exception-class/)
 * Hello,
 * I am using wp_error class to handle errors.
    Also when I use PHP throw new Exception,
   I find no way to pass an error code from wp_error() to Exception class. Exception
   class takes second parameter as getCode(). However the error codes from wp_error()
   class are in string form. Is there any wordpress way to handle such issue? Or
   the only option we have is to extend exception class?
 *     ```
       try{
   
               $error = new WP_Error();
   
               //Some validation
   
               if(is_wp_error()){
                   $error_code = $error->get_error_code(); //returns "my-error-code" (String)
                   throw new Exception( "Error thrown"); //How to send error code
               }   
           } catch{
               //Receive error code here
           }
       ```
   
 * Thanks a lot !

Viewing 2 replies - 1 through 2 (of 2 total)

 *  Moderator [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [7 years, 1 month ago](https://wordpress.org/support/topic/how-to-pass-error-code-from-wp_error-to-php-exception-class/#post-11472028)
 * Essentially, WP error “codes” are not compatible with Exception codes. I suppose
   you could create some sort of lookup table for common error strings and assign
   an appropriate code. Default to `0` if there is no match. WP_Error codes are 
   completely arbitrary and failure to match is likely.
 * Or you could concatenate both WP_Error code and message together to pass as the
   Exception message and simply use code `0` for all errors. The best approach depends
   on what you plan to do to handle errors.
 *  Thread Starter [xootixsupport](https://wordpress.org/support/users/xootixsupport/)
 * (@xootixsupport)
 * [7 years ago](https://wordpress.org/support/topic/how-to-pass-error-code-from-wp_error-to-php-exception-class/#post-11535907)
 * I am really, I forgot to check “Notify me of follow-up replies via email”
    The
   problem comes when you are dealing with other plugins & they throw the wp error
   codes. So it is essential to know the code value. Anyway, I created the custom
   exception class from which I can pass my custom parameter. Thanks for your help.

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘How to pass error code from wp_error() to PHP exception class?’ is closed
to new replies.

## Tags

 * [exception](https://wordpress.org/support/topic-tag/exception/)
 * [php](https://wordpress.org/support/topic-tag/php/)
 * [wp_error](https://wordpress.org/support/topic-tag/wp_error/)

 * In: [Developing with WordPress](https://wordpress.org/support/forum/wp-advanced/)
 * 2 replies
 * 2 participants
 * Last reply from: [xootixsupport](https://wordpress.org/support/users/xootixsupport/)
 * Last activity: [7 years ago](https://wordpress.org/support/topic/how-to-pass-error-code-from-wp_error-to-php-exception-class/#post-11535907)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
