0

Here's the code:

try{
//The exception is thrown.
throw new Exception('Parâmetros de consulta inválidos');

// and in the catch block it's caught successfully:
}catch(Exception $e){
    echo $e->getMessage(); //This prints the message correctly.

    $output = json_encode(array('msg'=>$e->getMessage()));
    echo $output; //But this fails...displays {"msg":null}

}

What is the issue here?

2
  • Seems to work codepad.org/6hvr4CLP Commented Oct 20, 2011 at 13:46
  • @jprofitt I just love when it works everywhere but on my machine... Commented Oct 20, 2011 at 13:49

2 Answers 2

2

The problem is with the character â and á. Infact, if you replace them with a simple a, you'll get the right message.

Replace this line:

array('msg'=>$e->getMessage())

with this:

array('msg'=>utf8_encode($e->getMessage()))

You have to do this change because the json_encode works with ut8 as you can read here.

Sign up to request clarification or add additional context in comments.

2 Comments

@Jorge Glad to help you. Anyway I've update my answer so you can also echo your special chars. ;)
I always have issues with the encoding, once i managed to print chinese characters, don't ask me how because i don't know. (Was using Chrome)
1

If you use a PHP version >= 5.4.0, you should call to the json_encode function with the JSON_UNESCAPED_UNICODE flag. http://php.net/manual/en/function.json-encode.php

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.