PHP 中 try-catch 和 if-else 语句的区别

PHP 中使用trycatch处理异常,如 C++、Java 等其他语言。异常是程序本身可以处理的意外结果或程序的意外状态。为了在 PHP 中处理这种意外结果,使用了trycatch。有关更多详细信息,请访问PHP 中的异常处理

同样,PHP 也使用ifelse执行条件语句来处理决策场景。有关详细信息,请访问PHP | 做决定

PHP 中 'try-catch' 和 'if-else' 的区别:

'if''else' 是什么? 

  • if:它检查任何条件是否为“真”,如果为真,则执行if块内的代码。
  • else:如果if块检查的条件为“假” ,则else块执行其中的其他代码。
if(condition)
{......}
else
{......} 

或者

if(condition)
{......}
else if(condition)
{......}
else
{......}

什么是“尝试”“捕捉”? 

  • try:这是一段定义代码块的部分,用于测试代码在执行时是否产生意外结果。
  • catch:这是定义另一个代码块的部分,如果在 try 块中产生任何意外结果,则执行该代码块。实际上,这段代码处理了异常。 
     
try
{...condition...;
 ...condition..;
 .....;
 .....;
 .....;
}
catch (Exception)
{...handling exception...;}

错误处理:主要是if-else块用于使用条件检查来处理错误。if-else作为条件语句捕获错误。在许多情况下,在执行期间必须检查许多极端情况,但“if-else”只能处理定义的条件。在if-else中,条件是根据任务手动生成的。 

<?php

$g = "GeeksforGeeks";

// Checks for a condition if
// 'g'is null or not
if ($g != "") {
	echo $g;
}

else {
	echo "This is not the string";
}
?>

输出: 

GeeksforGeeks

try-catch块的情况下,它将检查系统在执行过程或任务期间产生的错误或异常。这些错误或异常不是手动生成的。try-catch处理易于阅读的异常。

<?php

// Exception handling function
function tryCatchException($b, $var) {
	
	try {
		echo "\nDivision is ", $b/$var;
		throw new Exception('denominator is 0');
		// If 'var' is zero then exception
		// is thrown
	}
	// Catch block will be executed if
	// any Exception has been thrown
	// by try block
	catch(Exception $e) {
		echo "\nException: ", $e->getMessage();
		// Print the Message passed
		// by the thrown statement
	}
}

// Exception will happened
tryCatchException(6, 0);

?>

输出: 

Runtime Errors : 
PHP Warning:  Division by zero in 
/home/1027ff9c161eb6503f545005908318fc.php on line 8
Division is INF
Exception: denominator is 0 
 

使用一个块来处理错误或异常:if-else的情况下,我们有一个对应于一个if块的else块,因此我们必须用一个else块定义每个if块来处理“假”条件。在if之后也可能没有任何else语句。

<?php

function rngeLaptop($a) {

	// Each 'if' with one 'else'
	if ($a == "Gaming") {
		echo "Range stated from 50000rs\n";
	}
	
	else if($a == "Education") {
		echo "Range stated from 25000rs\n";
	}

	else if($a == "Graphics works") {
		echo "Range stated from 55000rs\n";
	}
	
	else if($a == "Entertainment") {
		echo "Range stated from 18000rs\n";
	}
	else {
		echo "Not listed\n";
	}
}

rngeLaptop("Gaming");
rngeLaptop("Education");
rngeLaptop("Movie"); // Not listed
rngeLaptop("Entertainment");
rngeLaptop("Graphics works");

?>

输出: 

Range stated from 50000rs
Range stated from 25000rs
Not listed
Range stated from 18000rs
Range stated from 55000rs

try-catch的情况下,我们不必用catch定义每次尝试一个try中可以定义多个异常,为了捕获try块抛出的异常,可以有一个catch块。

<?php

// Exception handling function
function tryCatchException($b, $var) {
	try {
		
		// Checking 2 condition and throwing
		// all exceptions in one catch block
		if($var == 0) {
			throw new Exception('denominator is 0');
		}
		if($var < 0) {
			throw new Exception('denominator is a negative');
		}
		echo "\nDivision is ", $b/$var;
	}
		
	// Catch block will be executed if any
	// Exception has been thrown by try block
	catch(Exception $e) {
			
		// Print the Message passed by
		// the thrown statement
		echo "\nException: ", $e->getMessage();
	}
}

// Exception will happened
tryCatchException(6, -3);
tryCatchException(12, 0);
tryCatchException(15, 3);

?>

输出: 

Exception: denominator is a string
Exception: denominator is 0
Division is 5 

简要讨论 PHP 中 'try-catch' 和 'if-else' 的区别: 

if-elsetry-catch
它检查任何条件是否为真,如果为真,则执行 if 块内的代码,否则执行 else 块。“try”是定义代码的部分,用于测试代码在执行时是否产生意外结果,如果执行任何意外结果发现 catch 块来处理这种情况。
'if-else' 用于使用条件检查来处理不同的条件。在“try-catch”的情况下,系统将在执行过程或任务期间检查系统产生的错误或异常。
条件是在“if-else”中手动生成的。根据任务。'try-catch' 处理系统生成的错误,如数组越界、除以零等。
在“if-else”中,块内的条件和代码混合在一起,因此如果有很多“if-else”块,它就会变得不可读。在“try-catch”中,处理异常的代码以及要处理的异常是易于阅读的。
在“if-else”中,我们有一个 else 块对应于一个 if 块。或者我们需要使用命令“else if”定义另一个条件。在“try-catch”中,我们不必为每个“try”块定义一个“catch”块。
'if-else' 比 'try-catch' 耗时少。'try-catch' 比 'if-else' 更耗时。
'if-else' 在提供给程序的数据和条件之间进行通信。'try-catch' 在具有条件的数据和系统之间进行通信。
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值