RecoilUK Posted July 5, 2008 Share Posted July 5, 2008 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 More sharing options...
DarkWater Posted July 5, 2008 Share Posted July 5, 2008 Well, exceptions are a major part of nearly every programming language. If done properly, they're extremely useful. If they're not done properly, they're annoying. Link to comment https://forums.phpfreaks.com/topic/113382-spl-exceptions/#findComment-582518 Share on other sites More sharing options...
Bauer418 Posted July 5, 2008 Share Posted July 5, 2008 I agree. Exceptions can be useful but they can be extremely annoying and pointless. If your code is truly "full" of try catch blocks, you probably have too many. Link to comment https://forums.phpfreaks.com/topic/113382-spl-exceptions/#findComment-582525 Share on other sites More sharing options...
RecoilUK Posted July 5, 2008 Author Share Posted July 5, 2008 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 More sharing options...
Bauer418 Posted July 5, 2008 Share Posted July 5, 2008 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 More sharing options...
trq Posted July 6, 2008 Share Posted July 6, 2008 Just wondering what your thoughts are on the exception handling introduced with the SPL Exceptions are not part of the SPL, they are in fact a part of the core since php5. Link to comment https://forums.phpfreaks.com/topic/113382-spl-exceptions/#findComment-582611 Share on other sites More sharing options...
DarkWater Posted July 6, 2008 Share Posted July 6, 2008 Oh yeah, I knew I forgot to point something out. Thanks thorpe. I read it and was like "It's not part of the SPL but I'm too lazy to post it." =P Link to comment https://forums.phpfreaks.com/topic/113382-spl-exceptions/#findComment-582614 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.