lang/php

php7.1 Catching Multiple Exception Types

C/H 2017. 5. 31. 08:30

try {
   // Some code...
} catch (ExceptionType1 $e) {
   // Code to handle the exception
} catch (ExceptionType2 $e) {
   // Same code to handle the exception
} catch (Exception $e) {
   // ...
}
try {
   // Some code...
} catch (ExceptionType1 | ExceptionType2 $e) {
   // Code to handle the exception
} catch (\Exception $e) {
   // ...
}
class exept1 extends Exception
{
	function __construct($message, $code = 0, Exception $previous = null)
	{
		parent::__construct($message, $code, $previous);
	}
}

class exept2 extends Exception
{
	function __construct($message, $code = 0, Exception $previous = null)
	{
		parent::__construct($message, $code, $previous);
	}
}

try
{
	if($rand = 1){
		throw new exept1(123);
	}else{
		throw new exept2(143);
	}
}
catch(exept1 | exept2 $e)
{
	var_dump( $e );
}


반응형

'lang > php' 카테고리의 다른 글

Yii model get query  (0) 2017.08.25
PHP math-php  (0) 2017.06.01
brew php-xdebug  (0) 2017.05.29
Multi-library chart PHP  (0) 2017.05.26
botman PHP  (0) 2017.05.25