livethedead Posted May 30, 2012 Share Posted May 30, 2012 Ok so I'm pretty new at this can't seem to figure out why I keep getting: Parse error: syntax error, unexpected T_TRY, expecting T_FUNCTION function __construct($input) { if (in_array($this->_input_word, Library::$_translators )) { $this->_input_word = $input; $this->_check = true; } else { throw new Exception("Word not found in library."); } } try {} catch(Exception $e) { echo $e->getMessage(); die(); } Link to comment https://forums.phpfreaks.com/topic/263360-exeception-help/ Share on other sites More sharing options...
scootstah Posted May 30, 2012 Share Posted May 30, 2012 You can't put random code inside a class definition; only methods and class variables are acceptable. Also, that's not really how you use Exceptions. You shouldn't be handling the Exception in the same place you throw it, that defeats the purpose. It should be more like: class Example { function __construct($input) { if (in_array($this->_input_word, Library::$_translators )) { $this->_input_word = $input; $this->_check = true; } else { throw new Exception("Word not found in library."); } } } try { $example = new Example; } catch(Exception $e) { echo $e->getMessage(); } Link to comment https://forums.phpfreaks.com/topic/263360-exeception-help/#findComment-1349693 Share on other sites More sharing options...
livethedead Posted May 30, 2012 Author Share Posted May 30, 2012 Ahhh ok I see, I was looking at exceptions in the manual and just kinda rolled with it without realizing I was just writing code inside a class. Thanks for pointing that out, I was about to pull my hair out. Link to comment https://forums.phpfreaks.com/topic/263360-exeception-help/#findComment-1349694 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.