Skip to content
Advertisement

Why isn’t my PHP exception working?

I am new to exceptions in PHP or any language really. I am trying to catch an exception if a user enters an invalid textual timezone (“xxxxxxxxxx” in this case). My test case is definitely invalid as an exception is triggered, just not the catch logic which is supposed to handle it intelligently. Basically I want it to use a valid timezone string if an invalid one is entered.

JavaScript

I get back:

xxxxxxxxxx~Mark D Hamill

Fatal error: Uncaught exception ‘Exception’ with message ‘DateTimeZone::__construct(): Unknown or bad timezone (xxxxxxxxxx)’ in /Applications/XAMPP/xamppfiles/apps/phpbb/htdocs/ext/phpbbservices/digests/cron/task/digests.php:1938 Stack trace: #0 /Applications/XAMPP/xamppfiles/apps/phpbb/htdocs/ext/phpbbservices/digests/cron/task/digests.php(1938): DateTimeZone->__construct(‘xxxxxxxxxx’) #1 /Applications/XAMPP/xamppfiles/apps/phpbb/htdocs/ext/phpbbservices/digests/cron/task/digests.php(514): phpbbservicesdigestscrontaskdigests->make_tz_offset(‘xxxxxxxxxx’, ‘Mark D Hamill’) #2 /Applications/XAMPP/xamppfiles/apps/phpbb/htdocs/ext/phpbbservices/digests/cron/task/digests.php(157): phpbbservicesdigestscrontaskdigests->mail_digests(1458353337, 0) #3 /Applications/XAMPP/xamppfiles/apps/phpbb/htdocs/ext/phpbbservices/digests/acp/main_module.php(1427): phpbbservicesdigestscrontaskdigests->run() #4 /Applications/XAMPP/xamppfiles/apps/phpbb/htdocs/includes/functions_module.php(674): phpbbservicesdigestsacpmain_module-> in /Applications/XAMPP/xamppfiles/apps/phpbb/htdocs/ext/phpbbservices/digests/cron/task/digests.php on line 1938

Line 1938 is where the error should be caught:

JavaScript

Advertisement

Answer

It looks like the code snippet above is inside a namespace. Consider the following code:

JavaScript

The solution to this is to explicitly specify it by changing your code to be something like this:

JavaScript

Further reading:

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement