Exception Handling with php

Exception Handling with php

Hi There,

I want to ask how to handle some exception in middle of the process (I'm working with PHP).

my js is :
  1. $(function(){
  2. var request;
  3. $('.naviref').on('click',function(){
  4. if(request){request.abort();}
  5. request = $.ajax({
  6. url: "html.php",
  7.        type: "post"
  8.     });

  9. request.done(function (response, textStatus, jqXHR){
  10. alert(response);
  11.         var text = JSON.parse(response);
  12.         alert(text);
  13.         if(text.error){
  14.         alert(1);}
  15. });

  16. request.fail(function (jqXHR, textStatus, errorThrown){
  17. alert('Error while making the request!');
  18. });

  19. event.preventDefault();
  20. });
  21. });

while in the php, the scenario is response the exception in the middle of the process :
  1. try{
  2. $a=20;
  3. $a=21;
  4. echo "the first letter : $a <br/>";
  5. throw new Exception("This is ERROR",0);
  6. echo "the second letter : $b <br/>";
  7. }
  8. catch(Exception $e){
  9. echo json_encode(array(
  10. 'error' => array(
  11. 'msg' => $e->getMessage()
  12. ),
  13. ));
  14. }


while test this code, alert output is 
the first letter : 21 <br/>{"error":{"msg":"This is ERROR"}}


is there possible to check if there is an error in jquery and parse to another page (exception information page) ?