当前位置:网站首页>PHP 中 try-catch 和 if-else 语句的区别
PHP 中 try-catch 和 if-else 语句的区别
2022-07-23 14:29:00 【allway2】
PHP 中使用try和catch处理异常,如 C++、Java 等其他语言。异常是程序本身可以处理的意外结果或程序的意外状态。为了在 PHP 中处理这种意外结果,使用了try和catch。有关更多详细信息,请访问PHP 中的异常处理。
同样,PHP 也使用if和else执行条件语句来处理决策场景。有关详细信息,请访问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-else | try-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
- Function secondary development / plug-in development of JMeter (detailed version)
- Priyanka Sharma, general manager of CNCF Foundation: read CNCF operation mechanism
- 【Flutter -- 布局】线性布局(Row 和 Column)
- 本周投融报:Web3游戏熊市吸金
- PMP practice once a day | don't get lost in the exam -7.23
- 程序环境和预处理
- 腾讯撕开中国NFT的“遮羞布”
- Pinduoduo app product details interface to obtain activity_ ID value (pinduoduo activity_id interface)
- IR drop, EM, noise and antenna
猜你喜欢

低代码搭建校园信息化管理系统案例分析

Keil errors and solutions (1): fcarm - output name not specified, please check 'options for target - Utilities‘

Is PMP a great help for practical work?

JS if the decimal is 0, subtract it, not keep it

拼多多APP商品详情接口获取activity_id值(拼多多activity_id接口)

Pymoo学习 (4): 多标准决策

Win11如何添加图片3D效果?Win11添加图片3D效果的方法

Récursion des bosses 1: formule récursive

Pyinstaller+InstallForge多文件项目软件打包

详解一次SQL优化
随机推荐
keras——accuracy_ Score formula
零基础怎么自学软件测试?十年测试老鸟最强软件测试学习路线图
Pinduoduo app product details interface to obtain activity_ ID value (pinduoduo activity_id interface)
Could not load dynamic library ‘cudnn64_8.dll‘; dlerror: cudnn64_8.dll not found
When does MySQL use table locks and row locks?
串的初步认识
Lake Shore—EMPX-H2 型低温探针台
Advanced authentication of uni app [Day12]
C语言基础篇 —— 2-6 指针、数组与sizeof运算符
Nodejs implements token login registration (koa2)
Major upgrade of openim - group chat reading diffusion model release group management function upgrade
Dead beat recursion 1: recursive formula
pip报错Could not find a version that satisfies the...No matching distribution
Opencv open camera, edge detection
[web vulnerability exploration] SQL injection vulnerability
[untitled]
腾讯撕开中国NFT的“遮羞布”
[MySQL]一、MySQL起步
IDEA中给项目添加依赖的jar包
树