?
Solved

PHP How to show error message in "catch"

Posted on 2012-08-24
3
Medium Priority
?
402 Views
Last Modified: 2012-08-26
I have something like this:

function doThis() {
Try {
  //lots of stuff
  return $thething;
} catch (SpecialException Exception $ex) {
			die($ex->getMessage());
}	
};

Open in new window


And calling code:

if ($_SERVER['REQUEST_METHOD'] == 'POST') {

$theThing = doThis();
//do stuff with $theThing;

}

Open in new window


How do I print the error upon die?

thanks!
0
Comment
Question by:christamcc
3 Comments
 
LVL 8

Accepted Solution

by:
Barry62 earned 1600 total points
ID: 38331408
Use die() as an empty function and echo the error before that.

function doThis() {
Try {
  //lots of stuff
  return $thething;
} catch (SpecialException Exception $ex) {
  echo $ex->getMessage();			
  die();
}	
};
                                  

Open in new window

0
 
LVL 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 400 total points
ID: 38331552
Please read this over and see if it makes sense to you.  If not, please post back and I will try to help with any questions.

<?php // RAY_try_throw_catch.php
error_reporting(E_ALL);
echo "<pre>";

// DEMONSTRATE A TRY WITH MULTIPLE CATCH BLOCKS
// http://php.net/manual/en/reserved.exceptions.php

Class ScrewUP
{
    public function __construct()
    {
        $this->code = rand(1,3);
    }

    public function hiccup()
    {
        if ($this->code == 1) throw new Exception_one;
        if ($this->code == 2) throw new Exception_two;
        throw new Exception;
    }
}

Class Exception_One extends Exception
{
    public function __construct()
    {
        echo "Exception One ";
    }
}

Class Exception_Two extends Exception
{
    public function __construct()
    {
        echo "Exception Two ";
    }
}

$x = new Screwup(2);
try
{
    $x->hiccup();
}
catch (exception_one $e)
{
    echo "Caught 1 ";
    var_dump($e);
}
catch (exception_two $e)
{
    echo "Caught 2 ";
    var_dump($e);
}
catch (exception $e)
{
    echo "Caught UnExtended ";
    var_dump($e);
}

Open in new window

Best regards, ~Ray
0
 
LVL 12

Expert Comment

by:Mohamed Abowarda
ID: 38333649
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses
Course of the Month16 days, 15 hours left to enroll

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question