>  기사  >  php教程  >  PHP에서 예외가 발생했습니다.

PHP에서 예외가 발생했습니다.

WBOY
WBOY원래의
2016-08-15 16:49:491040검색
다음으로 이동 [1] [전체 화면 미리보기]
<?php

/**
 * 错误异常处理
 */

$arr = [
	
	'data' => 'hello world',
];


$res = '123';

printData(check($res));

printData(check($arr));


/**
 * Array
(
[line] => 21
[file] => 21
[msg] => not is array
)
Array
(
[data] => hello world
)
 *
 */

function check($x){

	try{
		if(!is_array($x)) {

			throw new Exception('not is array');
		}

	}catch(Exception $e){

		$data['line'] = $e->getLine();
		$data['file'] = $e->getLine();
		$data['msg'] = $e->getMessage();
		return $data;
	}

	return $x;
}

$item = '123';

$row = [
	'0'=>1,
];


print_r(checkString($item));

print_r(checkString($row));

/*
 * Fatal error:  Uncaught Exception: 不是字符串 in D:\xampp\htdocs\phperror.php:77
Stack trace:
#0 D:\xampp\htdocs\phperror.php(62): checkString(Array)
#1 {main}
  thrown in D:\xampp\htdocs\phperror.php on line 82


Array
(
    [0] => 1
)
 */

function checkString($y){

	if(!is_string($y)){

		throw new Exception('不是字符串');
	}

	return $y;

}


function printData($data){

	echo '<pre class="brush:php;toolbar:false">';

	print_r($data);
}
성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.