Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP web application when to use try/catch

I am using PHP with CodeIgniter framework. I read some articles that stating using try/catch methods is in bad practice.

I understand to use logging in development when a potential error can occur and let the actual error happen then log it using log_message('level', 'message')

But when in deployment you want to suppress and handle any errors that arise. Should I be using try/catch blocks, for instance in something like...

try { 
    $this->data['important'] = $this->Test_Model->do_something($data);
    if(empty(data['important'])) {
    throw new Exception('no data returned');
}

catch (Exception $e) {
    //alert the user then kill the process
    var_dump($e->getMessage());
}

I'm confused when I use be using try/catch correctly.

like image 678
Edward Avatar asked Dec 18 '13 23:12

Edward


People also ask

What is the use of try catch in PHP?

This is exactly what try/catch is used for in PHP. try { // Code to attempt } catch (Exception $e) { // Code to execute if there's an error } // Execution resumes here, only if there was no error! A finally block can be optionally specified after your catch block.

How do I throw an exception in a PHP try-catch block?

<?php try { echo "Hello World!"; } catch (Exception $e) { echo "Whoops, something went wrong!"; } The above script will output Hello World! and everything within the catch block will be ignored. But, things get more interesting when your code within the try block throws an exception. For example:

What is the difference between try and catch in C++?

In this syntax, the try...catch statement has two blocks: try and catch. In the try block, you do some tasks e.g.,reading a file. If an exception occurs, the execution jumps to the catch block. In the catch block, you specify the exception name and the code to handle a specific exception.

What is the difference between throw and catch in PHP?

Throw: The throw keyword is used to signal the occurrence of a PHP exception. The PHP runtime will then try to find a catch statement to handle the exception. Catch: This block of code will be called only if an exception occurs within the try code block.


1 Answers

There's a syntax error in your code, it should be:

try { 
  $this->data['important'] = $this->Test_Model->do_something($data);
  if(empty($this->data['important'])) {
    throw new Exception('no data returned');
  }
} catch (Exception $e) {
  //alert the user.
  var_dump($e->getMessage());
}

Using try/catch isn't a bad practice.

Exceptions are catch-able without needing to override PHP default error handler. This means that you can do something when exceptions occurs in run-time.

Logs are for you. If your application isn't healthy and you logged possible problematic points, you can identify errors more easily. The main difference between your log and native log is the context, native log doesn't have context, they have just the occurrence and possibly stack trace.

like image 193
Andrey Avatar answered Oct 20 '22 10:10

Andrey