Exceptions - final - 3
The snippet can be accessed without any authentication.
Authored by
Krzysztof Dyszczyk
Edited
exception.php 745 B
class WPDesk_Post_Not_Exists_Exception extends \Exception {
}
/**
* @return \WP_Post
* @throws \WPDesk_Post_Not_Exists_Exception
*/
function get_wpdesk_post() {
$post = get_post(123);
if ($post) {
return $post;
}
throw new WPDesk_Post_Not_Exists_Exception('WPDesk post 123 is not available in the database');
}
/**
* @return string
* @throws \WPDesk_Post_Not_Exists_Exception
*/
function get_wpdesk_post_title() {
return get_wpdesk_post()->post_title;
}
/**
* @throws \WPDesk_Post_Not_Exists_Exception
*/
function display_wpdesk_title() {
echo get_wpdesk_post_title();
}
try {
display_wpdesk_title();
} catch (\WPDesk_Post_Not_Exists_Exception $not_exists) {
echo 'Some placeholder title';
}
Please register or sign in to comment