2016-03-19 13 views
0

私はPHPやその他の言語の例外を初めて熟知しています。私は、ユーザーが無効なテキストのタイムゾーン(この場合は "xxxxxxxxxx")を入力した場合、例外をキャッチしようとしています。私のテストケースは、例外がトリガされたときには間違いなく無効です。ちょうどそれを知的に処理するはずのキャッチロジックではありません。無効なものが入力された場合は、基本的に有効なタイムゾーン文字列を使用します。なぜPHPの例外が機能しないのですか?

echo $tz_text . '~' . $username . '<br />'; 
try 
{ 
    $tz = new \DateTimeZone($tz_text); 
} 
catch (Exception $e) 
{ 
    // Handles the issue of a timezone not being correct, see http://php.net/manual/en/timezones.php 
    if ($this->config['phpbbservices_digests_enable_log']) 
    { 
     $this->phpbb_log->add('admin', $this->user->data['user_id'], $this->user->ip, 'LOG_CONFIG_DIGESTS_TIMEZONE_ERROR', array($tz_text, $username, $this->config['board_timezone'])); 
    } 
    $tz = new \DateTimeZone($this->config['board_timezone']); 
} 

私は戻って取得:

XXXXXXXXXX〜マーク・D・ハミル

致命的なエラー:メッセージでキャッチされない例外 '例外' 'DateTimeZoneを:: __構築物():未知のか悪いのタイムゾーン(XXXXXXXXXX') /Applications/XAMPP/xamppfiles/apps/phpbb/htdocs/ext/phpbbservices/digests/cron/task/digests.php:1938スタックトレース:#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):phpbbser//アプリケーション/ XAMPP/xamppfiles/apps/phpbb/htdocs/ext/phpbbservices/digests/cron/task/digestsを使用してください。 php(157):phpbbservices \ digests \ cron \ task \ digests-> mail_digests(1458353337、0)#3 /Applications/XAMPP/xamppfiles/apps/phpbb/htdocs/ext/phpbbservices/digests/acp/main_module.php(1427) ):phpbbservices \ digests \ cron \ task \ digests-> run()#4 /Applications/XAMPP/xamppfiles/apps/phpbb/htdocs/includes/functions_module.php(674):phpbbservices \ digests \ acp \ main_module->ライン1938上の/Applications/XAMPP/xamppfiles/apps/phpbb/htdocs/ext/phpbbservices/digests/cron/task/digests.php

ライン1938は、エラーがキャッチされなければならないところです。

$tz = new \DateTimeZone($tz_text); 

答えて

1

上記のコードスニペットは名前空間内にあるようです。次のコードを考えてみましょう -

<?php 

namespace Foo/Bar; 

try { 
    ... 
} catch (Exception $e) { // This is trying to catch Foo/Bar/Exception 
    ... 
} 

ソリューションこれに明示的にこのようなものにするようにコードを変更することで、それを指定する -

try { 
    .... 
} catch (\Exception $e) { 
    .... 
} 

さらに読書 - http://php.net/manual/en/language.namespaces.php http://php.net/manual/en/language.namespaces.basics.php

ラーフル

+1

ありがとうございます。これらの問題の多くのように、顔の中で私を見て! – Mark

関連する問題