Skip to content
Snippets Groups Projects

Exceptions - final - 3

  • Clone with SSH
  • Clone with HTTPS
  • Embed
  • Share
    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';
    }
    0% Loading or .
    You are about to add 0 people to the discussion. Proceed with caution.
    Please register or to comment