Exception methods

Methods of exceptionobjects

getMessage()
getCode()
getFile()
getLine()
getTrace()
getTraceAsString()

The syntax for the try/catch statement

try { 
 // statements 
}
catch (ExceptionClass $exceptionName) { statements }
  [ catch (ExceptionClass $exceptionName) { 
    // statements 
} ]...

A statement that catches an Exception object

try {
  $fv =  calculate_future_value(10000, 0.06, 0);
  echo 'Future value was calculated successfully.';
} catch (Exception $e) {
  echo 'Exception: ' . $e->getMessage();
}

A statment that rethrows an exception

try {
  $fv = calculate_future_value(
  $investment, $annual_rate, $years);
} catch (Exception $e) {
  throw $e;
}

A statement that catches two types of exceptions

try {
  $db = new PDO($dsn, 'mmuser', 'pa55word', $options);
  // other statements
} catch (PDOException $e) {
  echo 'PDOException: ' . $e->getMessage();
} catch (Exception $e) {
  echo 'Exception: ' . $e->getMessage();
}

Back