katie hudson
katie hudson

Reputation: 2893

PHP when to use try/catch?

I was just wondering something. In index.php, I am currently doing something like this

function performFtpOperation() {

    global $config;

    try {
        $ftp = new FTP\FtpClient();
        $ftp->connect($config::FTP_SERVER);
        $ftp->login($config::FTP_USER, $config::FTP_PASSWORD);

    } catch (Exception $e) {
        echo 'Error: ', $e->getMessage();
    }
}

What I was wondering is if that try catch block is needed? Reason I question it is because my FTP class throws errors if something goes wrong. For instance this is the connect function

public function connect($host, $ssl = false, $port = 21, $timeout = 90)
{
    if ($ssl) {
        $this->conn = @$this->ftp->ssl_connect($host, $port, $timeout);
    } else {
        $this->conn = @$this->ftp->connect($host, $port, $timeout);
    }
    if (!$this->conn) {
        throw new Exception('Unable to connect');
    }
    return $this;
}

So would a try/catch be needed if errors are handled within the class?

Thanks

Upvotes: 0

Views: 1726

Answers (4)

Racil Hilan
Racil Hilan

Reputation: 25371

The connect class throws exceptions that you need to catch and handle somewhere in your code. It is up to you where to handle it depending on your application design and requirements.

If you decided that you wanted to handle them in the performFtpOperation function, then your use of try...catch there is correct.

If you don't handle them in the performFtpOperation function, then they will bubble up to the code that calls the performFtpOperation function and you can catch & handle them there if you like using try...catch similar to how you did it here. Just remember that you need to catch them somewhere.

Upvotes: 2

Broatcast
Broatcast

Reputation: 140

You need to try catch if you work with exeptions, otherwise you generate fatal errors and your script/site will stop work at this point, so try/catch is a part of the exeption handling.

you can inform yourself about this in the php manual

http://php.net/manual/en/language.exceptions.php

Upvotes: 1

Dinanath Thakur
Dinanath Thakur

Reputation: 686

When an exception is thrown, the code following it will not be executed, and PHP will try to find the matching "catch" block. If an exception is not caught, a fatal error will be issued with an "Uncaught Exception" message.

Proper exception code should include:

  • Try - A function using an exception should be in a "try" block. If the exception does not trigger, the code will continue as normal. However if the exception triggers, an exception is "thrown"
  • Throw - This is how you trigger an exception. Each "throw" must have at least one "catch"
  • Catch - A "catch" block retrieves an exception and creates an object containing the exception information

    Upvotes: 5

  • leeor
    leeor

    Reputation: 17811

    Well since you are throwing an exception from within your FTP code, then Yes.

    Upvotes: 1

    Related Questions