2016-03-19 20 views
0

PHP veya herhangi bir dilde istisnalar konusunda gerçekten yeniyim. Bir kullanıcı geçersiz bir metinsel saat dilimi (bu durumda "xxxxxxxxxx") girerse, bir özel durum yakalamaya çalışıyorum. Test durumum kesinlikle bir istisna tetiklendiğinde geçersizdir, akıllıca ele alması gereken yakalama mantığını değil. Temel olarak geçersiz bir girilirse geçerli bir saat dilimi dizesi kullanmasını istiyorum.Neden benim PHP istisnam çalışmıyor?

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']); 
} 

Ben dönene:

xxxxxxxxxx ~ Mark D Hamill

Önemli hata: iletisiyle yakalanmamış özel durum 'İstisna' ':: __ construct() DateTimeZone: Bilinmeyen ya da kötü zaman dilimi (xxxxxxxxxx)' /Applications/XAMPP/xamppfiles/apps/phpbb/htdocs/ext/phpbbservices/digests/cron/task/digests.php:1938 Yığın izleme: # 0/Uygulamalar/XAMPP/xamppfiles/uygulamalar/phpbb/htdocs/ext/phpbbservices /digests/cron/task/digests.php(1938): DateTimeZone -> __ yapı ('xxxxxxxxxx') # 1/Uygulamalar/XAMPP/xampp dosyaları/uygulamalar/phpbb/htdocs/ext/phpbbservices/digests/cron/task/digests. php (514): phpbbser vices \ digests \ cron \ task \ digests-> make_tz_offset ('xxxxxxxxxx', 'Mark D Hamill') # 2/Uygulamalar/XAMPP/xampp dosyaları/uygulamalar/phpbb/htdocs/ext/phpbbservices/digests/cron/task/digests. php (157): phpbbservices \ digests \ cron \ task \ digests-> mail_digests (1458353337, 0) # 3 /Uygulamalar/XAMPP/xamppfiles/apps/phpbb/htdocs/ext/phpbbservices/digests/acp/main_module.php(1427): phpbbservices \ digests \ cron \ task \ digests-> run() # 4 /Uygulamalar/XAMPP/xamppfiles/apps/phpbb/htdocs/includes/functions_module.php(674): phpbbservices \ digests \ acp \ main_module-> içinde /Applications/XAMPP/xamppfiles/apps/phpbb/htdocs/ext/phpbbservices/digests/cron/task/digests.php hattında hattı 1938 1938

hata yakalandı edilmelidir nerede:

$tz = new \DateTimeZone($tz_text); 

cevap

1

Yukarıdaki kod parçacığının bir ad alanının içinde olduğu anlaşılıyor. Böyle bir şey olması

<?php 

namespace Foo/Bar; 

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

bu çözüm açıkça kodunuzu değiştirerek belirtmek için - - Aşağıdaki kodu düşünün

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

fazla okuma - http://php.net/manual/en/language.namespaces.php http://php.net/manual/en/language.namespaces.basics.php

Rahul

+1

Teşekkür ederiz. Bu konuların birçoğu gibi, yüzüne bakıyorum! – Mark