SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Mar 2007
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Why is this code generating an "Uncaught Exception" error?

    ************************************
    <?php

    ini_set ('display_errors', 1);
    error_reporting (E_ALL & ~E_NOTICE);

    try
    {
    dothis();
    }
    catch(Exception $e)
    {
    echo $e->getMessage();
    exit();
    }

    function dothis()
    {
    throw new Exception("Not cool!");
    }

    ?>
    ************************************
    This block of code generates the error message:

    Fatal error: Uncaught exception 'Exception' with message 'Not cool!' in /home/goddess/public_html/except.php:19 Stack trace: #0 /home/goddess/public_html/except.php(9): dothis() #1 {main} thrown in /home/goddess/public_html/except.php on line 19


    Why?? I clearly have a catch block set up. Is it a problem with my code or with my server? If it's a problem with the server, is there an upgrade or a patch available that would fix it?

  2. #2
    SitePoint Addict miggl's Avatar
    Join Date
    Feb 2007
    Location
    Los Angeles, CA
    Posts
    286
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The problem here is that you are throwing an exception outside of the try-catch block. The exception is occuring while the function is being executed, meaning that you will have to have a try-catch in the function as well.'
    The try-catch block is only effective in its current scope, and won't 'travel' if you call a function or class. (That is my current understanding.)

    Try adding a try-catch block to the function and see if it correctly captures the error.
    Celebrate Liberty, Freedom, and Rights at The Constitutionalist.

  3. #3
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by thegoddessofelev View Post
    Is it a problem with my code or with my server? If it's a problem with the server, is there an upgrade or a patch available that would fix it?
    I see no problem in php 5.2. Which version are you using?

  4. #4
    SitePoint Addict miggl's Avatar
    Join Date
    Feb 2007
    Location
    Los Angeles, CA
    Posts
    286
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm looking at the php.net example:
    PHP Code:
    <?php
    try {
       
    $error 'Always throw this error';
       throw new 
    Exception($error);

       
    // Code following an exception is not executed.
       
    echo 'Never executed';

    } catch (
    Exception $e) {
       echo 
    'Caught exception: ',  $e->getMessage(), "\n";
    }

    // Continue execution
    echo 'Hello World';
    ?>
    I don't really see a difference to the example you provided in your other post. Perhpaps your php.ini error settings differ?

    Update: when I run this code I get the following response:
    Caught exception: Always throw this error Hello World
    When I run your script below I get the following response:
    Not cool!
    This tells me there is something else going on (your code is good).
    Celebrate Liberty, Freedom, and Rights at The Constitutionalist.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •