Maison > php教程 > PHP源码 > le corps du texte

php抛出异常

WBOY
Libérer: 2016-08-15 16:49:49
original
1036 Les gens l'ont consulté
跳至 [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);
}
Copier après la connexion
Étiquettes associées:
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Recommandations populaires
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!