try / catch assistance

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
hag
Forum Newbie
Posts: 10
Joined: Tue Apr 06, 2010 10:51 pm

try / catch assistance

Post by hag »

Why does the following code not work?

Code: Select all

<?php
try {
  include "anypage.php";
} catch(Exception $e) {
  echo 'Caught it';
}
?>
Hag
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: try / catch assistance

Post by requinix »

General Posting Guidelines

Ask yourself - you're the only one who knows what "not work" means.

Unless you feel like sharing that information...
hag
Forum Newbie
Posts: 10
Joined: Tue Apr 06, 2010 10:51 pm

Re: try / catch assistance

Post by hag »

it doesn't work because i need a break... this works

Code: Select all

<?php
try {
  if(!(@include 'anypage.php'))
  {
    throw new Exception();
  }
} catch(Exception $e) {
  echo 'Caught it!';
}
?>
Now time for chocolate milk....

Hag
hag
Forum Newbie
Posts: 10
Joined: Tue Apr 06, 2010 10:51 pm

Re: try / catch assistance

Post by hag »

I guess i meant why doesn't the catch block properly catch my try statement... i'm befuddled but now back on track... thanks for your assistance

Hag
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: try / catch assistance

Post by requinix »

include() will only issue a warning if the file couldn't be included. Warnings/errors and exceptions are two totally different things.

So if you want an exception you have to throw one yourself. Pretty much like you're doing now.
hag
Forum Newbie
Posts: 10
Joined: Tue Apr 06, 2010 10:51 pm

Re: try / catch assistance

Post by hag »

gotcha... what about require()... shouldn't it throw an exception?

Are there any built in PHP functions that will "include" a class file and throw and exception if it fails?

noob->Hag
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: try / catch assistance

Post by Weirdan »

hag wrote:gotcha... what about require()... shouldn't it throw an exception?
Are there any built in PHP functions that will "include" a class file and throw and exception if it fails?
Nope. You could convert warnings/notices to exceptions though, using set_error_handler() :

Code: Select all

class InternalErrorException extends Exception {
    private $errorFile = null;
    private $errorLine = null;
    public function __construct($code, $message, $file, $line) {
        parent::__construct($message, $code);
        if ($file) {
             $this->errorFile = $file;
        }
        if ($line) { 
             $this->errorLine = $line;
        }
    }

    public function __toString() {
         return "[{$this->getCode()}] {$this->getMessage()} ({$this->getErrorFile()}:{$this->getErrorLine()}) in \n{$this->getTraceAsString()}";
    }

    public function getErrorFile() {
         return is_null($this->errorFile) ? '[unknown]' : $this->errorFile;
    }

    public function getErrorLine() {
         return is_null($this->errorLine) ? '[unknown]' : $this->errorLine;
    }
}
set_error_hander(function($errno, $errstr, $errfile, $errline, $errcontext) { 
   if (error_reporting() & $errno) {
        throw new InternalErrorException($errno, $errstr, $errfile, $errline);
   }
}, E_ALL);
hag
Forum Newbie
Posts: 10
Joined: Tue Apr 06, 2010 10:51 pm

Re: try / catch assistance

Post by hag »

very nice.. thanks

Hag
Post Reply