I build some package with my own Exception but when I try to catch a exception I got fatal error: Uncaught exception. This situation is only when method with throw is passed by add_action( 'init', array( $this, 'wp_some_method' ) );
Example:
class SomeClass {
public function __construct() {
add_action( 'init', array( $this, 'wp_some_method' ) );
echo '__constructor<br />';
}
function some_method(){
throw new Exception('some message');
}
function wp_some_method( $post_type ){
throw new Exception('Some second error');
}
}
try{
echo 'try <br />';
$o = new SomeClass();
//$o->some_method(); - this throw exception correct
} catch (Exception $ex) {
echo $ex->getMessage();
}
Displayed on the screen:
try
__constructor
And:
Fatal error: Uncaught exception ‘Exception’
Your exception isn’t caught by the try{} catch(){} block, because it isn’t thrown inside the try catch block. This demonstrates a lack of understanding of asynchronous events and the WordPress hook/action/event system.
The methods of your object are attached to the init action hook, and are thrown when the init hook is fired, not when the object is created, and not when they’re attached.
e.g.
Your method is not called when your object is created. It’s attached to the init event yes, but it is not called, precisely because the ‘init’ event hasn’t happened yet. The init event happens long after your try{} catch statement runs.
So instead, these would be more appropriate:
Otherwise there is no rational, logical, common sense reason why the line of code that says
throw new Exception
should be executed inside the try catch block as you have above without you purposefully calling it manually as you did in your test.