📜  PHPtry-catch 和 if-else 语句的区别

📅  最后修改于: 2022-05-13 01:54:11.425000             🧑  作者: Mango

PHPtry-catch 和 if-else 语句的区别

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

同样, PHP也使用ifelse执行条件语句来处理决策场景。更多详情,请访问PHP |做决定

PHP“try-catch”“if-else”的区别:

什么是“如果”“其他”

  • if:它检查任何条件是否为“真”,如果为真则执行if块中的代码。
  • else:如果条件为“false”,由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 中,条件是根据任务手动生成的。

 

输出:

GeeksforGeeks

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

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
Divition is INF
Exception: denominator is 0 

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

 

输出:

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

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

getMessage(); 
    }
}
   
// Exception will happened
tryCatchException(6, -3); 
tryCatchException(12, 0);
tryCatchException(15, 3); 
   
?> 

输出:

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

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

if-elsetry-catch
It checks any condition is true or not, if true then execute the code inside the if block otherwise execute else block.‘try’ is a section where a code is defined for tested whether the code generates an unexpected result while executing, if any unexpected result found catch block is executed to handle that situation.
‘if-else’ use to handle different conditions using condition checking.In case of ‘try-catch’, the system will check the system generated errors or exception during a executing process or a task.
Conditions are manually generated in ‘if-else’. according to the task.‘try-catch’ handles system generated errors like, if a array is out of bound, divide by zero etc.
In ‘if-else’ the conditions and the codes inside the blocks are got mixed, so that it becomes unreadable if there is many ‘if-else’ blocks.In ‘try-catch’ the codes to handle the exceptions and what exception to be handled, that are easily readable.In ‘if-else’, we have one else block corresponding to one if block. Or we need to define another condition with command ‘else if’.In ‘try-catch’ we don’t have to define each ‘try’ block with a ‘catch’ block.
‘if-else’ is less time consuming than ‘try-catch’.‘try-catch’ is more time consuming than ‘if-else’.
‘if-else’ communicate between the data provided to the program and the condition.‘try-catch’ communicate between the data with the conditions and the system.