throw Exception in php

Friday, October 31, 2008 9:52
Posted in category PHP, Programming
 try {
   doExchange();
   throw new Exception("D'oh!");
   echo "hai";
  }
catch (Exception $except) {
    logger.error("doExchange failed", e);
}

In that example, PHP enters the “try” block and starts executing code. When it hits the line “throw new Exception”, it will stop executing the “try” block, and jump to the “catch” block. Here it checks each exception option against the list in “catch” and executes the appropriate code. Once PHP has left the “try” block, it will not return to it.

The $except variable after the Exception class is there because PHP actually hands you an instance of that Exception class, set up with information about the exception you’ve just experienced. As all exceptions extend from the base class Exception

Bookmark and Share
You can leave a response, or trackback from your own site.

Leave a Reply