<?php /** * Created by PhpStorm. * User: vasilepop * Date: 8/10/15 * Time: 4:39 PM */ //create function with an exception function inverse($x) { if (!$x) { throw new Exception('Division by zero.'); } return 1 / $x; } //trigger exception in a "try" block try { echo inverse(5) . "\n"; echo inverse(0) . "\n"; } catch (Exception $e) { echo 'Caught exception: ', $e->getMessage(), "\n"; } // Continue execution echo "Hello World\n";
echo "catch exception :", $exc->getMessage(), "\n"; } try { if ($e) { $c = null; $info = 'happen error!' . $e; throw new Exception($info); } } catch (Exception $exc) { echo "catch exception:", $exc->getMessage(), "\n"; } var_dump($c); exit; try { inverse(0); inverse(5); } catch (Exception $e) { echo 'caught exception : ', $e->getMessage(), "\n"; } finally { echo "woke"; } // finally 中的代码总会执行,无论是否有异常抛出 echo 'first try over ' . "\n"; try { inverse(3); } catch (Exception $e) { echo "second exception : ", $e->getMessage(), '\\n'; } finally { echo "my fike"; } echo "second try over " . "\n";
<?php function inverse($x) { //si $x=0 on lance une exception if (!$x) { throw new Exception('Division par zéro'); } return 1 / $x; } // bloc try pour permettre de lancer des exceptions try { echo '5: ' . inverse(5) . '<br/>'; echo '10: ' . inverse(10) . '<br/>'; // va provoquer une exception... echo '0: ' . inverse(0) . '<br/>'; } catch (Exception $e) { echo '<p style="color: red; background-color: yellow;">Oups: ' . $e->getMessage() . '</p>'; } //le code continue même en cas d'erreur echo 'Hello World!';
<?php function inverse($x) { if (!$x) { throw new Exception('Division by zero.'); } else { return 1 / $x; } } try { echo inverse(5) . PHP_EOL; echo inverse(0) . PHP_EOL; } catch (Exception $e) { echo 'Caught exception: ', $e->getMessage(), PHP_EOL; } // Continue execution echo 'Hello World' . PHP_EOL;
{ } function is_even($x) { if ($x == 0) throw new MyException("0是一个特别的数,它是偶数和奇数的分水岭", 1);//会优先寻找对应的异常类的catch捕捉,寻找基础异常类的捕捉或finally if ($x % 2 == 0) { return true; } else { return true; } } try { echo is_even(0), '<br/>';//这里已经抛出异常了 echo inverse(0), '<br/>'; } catch (MyException $e) { echo 'into MyException', '<br/>'; echo $e->getMessage(), '<br/>'; } catch (Exception $e) { echo 'into Exception', '<br/>'; echo $e->getMessage(), '<br/>'; }
<?php //Обработка исключений //Определяем функцию function inverse($x) { if ($x == 0) { //если аргумент = 0, то выбрасываем исключение throw new Exception("Деление на ноль!"); } else { return 1 / $x; } } //пробуем поймать исключение try { inverse(0); } catch (Exception $e) { echo "Выброшено исключение: " . $e->getMessage(); }
function inverse($x) { if (!$x) { throw new Exception('Division by zero.'); } return 1 / $x; } try { echo inverse(5), "\n"; echo inverse(0), "\n"; } catch (Exception $e) { echo 'Caught exception: ', $e->getMessage(), "\n"; } try { echo inverse(0), "\n"; } catch (Exception $e) { echo 'Caught exception: ', $e->getMessage(), "\n"; } finally { echo "Second finally.\n"; } // Continue execution echo "Hello World\n"; class MyException extends Exception { } class Test { public function testing() { try {
<?php class BTS2BException extends \Exception { public function __toString() { parent::__toString(); // TODO: Change the autogenerated stub return 'Error in ' . $this->getFile() . ', line :' . $this->getLine() . ' Trace: ' . $this->getTraceAsString(); } } function inverse($x) { if (!$x) { throw new BTS2BException('Division by zero'); } throw new Exception('coucou'); return 1 / $x; } try { inverse('trt'); } catch (BTS2BException $e) { echo $e; } catch (Exception $e) { echo 'toto'; }