2014-02-06 69 views
0

我有一個Laravel 4型號:的try catch語句不能捕獲自定義異常

Class Organisation extends Eloquent { 

.... 


/** 
* add user to organisation & assign permission group 
* 
* @param $user User user to add to organisation 
* @param $group String name of the permissions group to add the user to 
* @return void 
*/ 
public function addUser(User $user, $group = 'Editors') 
{ 
    // make sure the user is not already in the organisation 
    if(! $this->users->contains($user->id)) 
    { 
     // if organisation has no users, force first user as an admin 
     if(! $this->users->count()) 
      $group = 'Admins'; 

     if($group = $this->getGroup($group)) 
     { 
      if($user->addGroup($group) && $this->users()->attach($user)) 
       return true; 
     }     
    } 
    else 
     throw new UserExistsException($user->fullName()." already belongs to ".$this->title."."); 
} 

.... 
} 

還我一個控制器調用這個函數/型號:

/** 
* Manage users form processing 
* 
* @return Redirect 
*/ 
public function postIndex(Organisation $organisation) 
{ 
    if($user = Sentry::findUserByLogin(Input::get('email'))) 
    { 
     try 
     { 
      if($organisation->addUser($user, 'Editors')) 
       return Redirect::route('organisation-user-index', $organisation->id) 
        ->with('success', '<strong>' . $user->fullName() . '</strong> successfully added.'); 
     } 
     catch(Cartalyst\Sentry\Users\UserExistsException $e) 
     { 
      return Redirect::route('organisation-user-index', $organisation->id) 
       ->with('error', $e); 
     } 
    } 

    return Redirect::route('organisation-user-index', $organisation->id) 
     ->with('error', 'Something went wrong. Try again.'); 
} 

爲什麼抓statment沒有得到越來越發生這種情況的例外情況?而只是簡單地被拋出......而沒有被抓住?

回答

4

你不顯示所有的代碼,但看起來你正在使用的命名空間,所以你可能要:

catch(\Cartalyst\Sentry\Users\UserExistsException $e) 

而不是

catch(Cartalyst\Sentry\Users\UserExistsException $e) 
+0

就是這樣。我忘了添加尾部斜線。尾部的斜槓會將我的命名空間範圍恢復爲根。謝謝! – AndrewMcLagan