Question

I have:

<?php

    try 
    {
        throw new Exception('Debes iniciar sesión para esto.', 1); // This is a null exception.
        throw new Exception('Debes iniciar sesion para esto.', 1); // This is not a null exception.
    }
    catch (Exception $ex1) 
    {
        echo 'Message: ' . $ex1->getMessage();
    }

When I throw the first exception it results null!! and I can't get the message when handling it! but for some reason if I remove the accent it throws the message correctly.

What could it be? a bug? (if you don't believe me, try it yourself)

Was it helpful?

Solution

It works: http://codepad.viper-7.com/MDs4NB. You're missing your echo statement to make the string and error message print out:

try 
{
    throw new Exception('Debes iniciar sesión para esto.', 1); // This is a null exception.
    throw new Exception('Debes iniciar sesion para esto.', 1); // This is not a null exception.
}
catch (Exception $ex1) 
{
    echo 'Message: ' . $ex1->getMessage();
}

OTHER TIPS

My bad, it has nothing to do with PHP, as John Conde said, the thing was that I forgot the use of json_encode with the exception, and json_encode doesn't like non-utf8 characters, so I had something like:

try 

{

 throw new Exception('Debes iniciar sesión para esto.', 1);

}

catch (Exception $ex1) 

{

 echo json_encode(array('message' => $ex1->getMessage()));

}

So, when json_encode detects non-utf8 data it will return NULL, so in order to fix this issue we have to explicity encode the data to utf8 before getting the json like this:

json_encode(array('message' => utf8_encode('Canción.')));

The result will generate a JSON with hex characters formated (i.e \x00), with their respective ASCII code. Sorry for telling it was PHP issue when it was a bad of mine. However, I'll accept the John's answer because he answered correctly and he deserves the credit, but I will also leave mine to provide the real cause of the problem.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top