Jump to content

SPL Exceptions?


RecoilUK

Recommended Posts

Hi guys

 

Just wondering what your thoughts are on the exception handling introduced with the SPL, the throw new exception, try catch block method.

 

I just tried it for the first time and it creates a total mess, what was once clean and concise code is now full of try catch blocks.

 

Anyone else agree? do you use it? if not, what other methods do you use?

 

Thanks guys.

Link to comment
https://forums.phpfreaks.com/topic/113382-spl-exceptions/
Share on other sites

OK

 

This is what I have had to do, the first two were require_once but that doesnt work with exceptions so I had to swap them for includes.

 

<?php

try {
  if (!@include_once $_SERVER['DOCUMENT_ROOT'].'/classes/template.class.php') {
    throw new Exception("EXCEPTION : REQUIRED CLASS - TEMPLATE - NOT FOUND!!");
  }
}
catch (Exception $e) {
  echo $e->getMessage();
  die();
}

try {
  if (!@include_once $_SERVER['DOCUMENT_ROOT'].'/classes/database.class.php') {
    throw new Exception("EXCEPTION : REQUIRED CLASS - DATABASE - NOT FOUND!!");
  }
}
catch (Exception $e) {
  echo $e->getMessage();
  die();
}

$template = Template::Instance();
$db = Database::Instance();

try {
  $template->SetLayout('layout.html');
} 
catch (Exception $e) {
  echo 'EXCEPTION : ',  $e->getMessage(), "<BR>", $e->getFile(), "<BR> Line Number - ", $e->getLine(), "\n";
  die();
}

?>

 

Looks like a mess right?

Link to comment
https://forums.phpfreaks.com/topic/113382-spl-exceptions/#findComment-582527
Share on other sites

Yes.  You don't need to use exceptions where you already have if statements.

 

This whole block:

try {
  if (!@include_once $_SERVER['DOCUMENT_ROOT'].'/classes/template.class.php') {
    throw new Exception("EXCEPTION : REQUIRED CLASS - TEMPLATE - NOT FOUND!!");
  }
}
catch (Exception $e) {
  echo $e->getMessage();
  die();
}

 

Will product the same results as this:

  if (!@include_once $_SERVER['DOCUMENT_ROOT'].'/classes/template.class.php') {
    print("EXCEPTION : REQUIRED CLASS - TEMPLATE - NOT FOUND!!");
    exit;
  }

Link to comment
https://forums.phpfreaks.com/topic/113382-spl-exceptions/#findComment-582529
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.