2012-09-06 38 views
1

當我從類中調用函數時,我無法得知自己的頭何時拋出並捕獲異常。處理一個類的函數的多個調用的例外

請想象一下,我QuizMaker類看起來是這樣的:

// Define exceptions for use in class 
private class CreateQuizException extends Exception {} 
private class InsertQuizException extends Exception {} 

class QuizMaker() 
{ 

    // Define the items in my quiz object 
    $quiz_id = null; 
    $quiz_name = null; 

    // Function to create a quiz record in the database 
    function CreateQuizInDatabase() 
    { 

     try 
     { 

      $create_quiz = // Code to insert quiz 

      if (!$create_quiz) 
      { 
       // There was an error creating record in the database 
       throw new CreateQuizException("Failed inserting record"); 
      } 
      else 
      { 
       // Return true, the quiz was created successfully 
       return true; 
      } 

     } 
     catch (CreateQuizException $create_quiz_exception) 
     { 
      // There was an error creating the quiz in the database 
      return false; 
     } 
    } 

    function InsertQuestions() 
    { 
     try 
     { 
      $insert_quiz = // Code to insert quiz 

      if (!$insert_quiz) 
      { 
       // There was an error creating record in the database 
       throw new CreateQuizException("Failed inserting quiz in to database"); 
      } 
      else 
      { 
       // Success inserting quiz, return true 
       return true; 
      } 
     } 
     catch (InsertQuizException $insert_exception) 
     { 
      // Error inserting question 
      return false; 
     } 
    } 
} 

......並使用此代碼,我使用類數據庫

class QuizMakerException extends Exception {} 

try 
{ 
    // Create a blank new quiz maker object 
    $quiz_object = new QuizMaker(); 

    // Set the quiz non-question variables 
    $quiz_object->quiz_name = $_POST['quiz_name']; 
    $quiz_object->quiz_intro = $_POST['quiz_intro']; 
      //... and so on ... 

    // Create the quiz record in the database if it is not already set 
    $quiz_object->CreateQuizRecord(); 

    // Insert the quiz in to the database 
    $quiz_object->InsertQuestions(); 

} 
catch (QuizMakerException $quiz_maker_error) 
{ 
    // I want to handle any errors from these functions here 
} 
創建一個新的測驗

對於這段代碼,我想調用QuizMakerException,如果任何函數不執行我想要的(當前它們返回TRUE或FALSE)。

什麼是正確當這段代碼中的任何函數沒有執行我所希望的任何功能時,會如何捕獲?目前他們只是返回TRUE或FALSE。

  • 難道我真的把大量的if/else語句的調用各功能之間,我認爲這是在異常的整點,他們只是停止的try/catch內進一步語句的執行?
  • 我是否在我的函數的catch內拋出QuizMakerException?

什麼是正確的做法?

幫助!

回答

2

那麼通常會拋出異常的功能,說出你的InsertQuestions方法,你不想捕獲任何異常,想要把他們或讓發生的「冒泡」的。然後你的「控制器」代碼可以決定如何處理異常。

如果您的目標是在CreateQuizRecord失敗時停止,我們會在各自的嘗試區塊中包括CreateQuizRecordInsertQuestions

異常的一個優點是它們可以告訴你不僅僅是一個簡單的bool合格/不合格。將基本異常擴展爲諸如「Invalid_Paramter」之類的東西,並測試特定的異常,或者從異常的屬性中推斷出最理想的異常。你可以嵌套你的catch塊來單獨處理異常。

我是否在我的函數中捕獲了QuizMakerException?

是的。通常你的代碼在// Code to insert quiz之下會自己返回一個異常。說如果模型未能插入它可能會引發數據庫異常。在這種情況下,你可以讓數據庫異常冒出來,或者做你現在正在做的事情,然後簡單地捕捉它,然後拋出另一個異常(雖然這樣做有點愚蠢)。

我真的不得不把大量的if/else語句的調用各功能之間,我認爲這是在異常的整點,他們只是停止的try/catch內進一步語句的執行?

我看起來就像這樣,每個調用引發異常,然後後續調用依賴於這個不引發任何異常的調用,應該包裝在try塊中。假設你想要優雅地處理它,如果你只是想讓它出錯並停止,就不要處理異常。你會得到一個錯誤和堆棧跟蹤。有時候這是可取的。

+0

我建議看看github上的一些代碼。搜索PHP項目並查看它們如何使用異常非常簡單。 Zend框架可能會提供一些很好的例子。 – ficuscr

0

要做的最好的事情就是不必首先排除異常。 程序崩潰時會引發異常,並且它們不會處理錯誤的輸出。 如果你的函數返回任何東西(即使它是錯誤的東西),它也不需要異常。

要回答你的問題,如果有必要使用大量的ifs/elses,那麼你必須使用它們。

2

你可能想改變一下結構。類QuizMaker可以成爲:

<?php 

// Define exceptions for use in class 
public class CreateQuizException extends Exception {} 
public class InsertQuizException extends Exception {} 

class QuizMaker() 
{ 

    // Define the items in my quiz object 
    $quiz_id = null; 
    $quiz_name = null; 

    // Function to create a quiz record in the database 
    function CreateQuizInDatabase() 
    { 

     try 
     { 

      $create_quiz = // Code to insert quiz 

      if (!$create_quiz) 
      { 
       // There was an error creating record in the database 
       throw new CreateQuizException("Failed inserting record"); 
      } 
      else 
      { 
       // Return true, the quiz was created successfully 
       return true; 
      } 

     } 
     catch (Exception $create_quiz_exception) 
     { 
      // There was an error creating the quiz in the database 
      throw new CreateQuizException(
       "Failed inserting record " . 
       $create_quiz_exception->getMessage() 
      ); 
     } 
    } 

    function InsertQuestions() 
    { 
     try 
     { 
      $insert_quiz = // Code to insert quiz 

      if (!$insert_quiz) 
      { 
       // There was an error creating record in the database 
       throw new InsertQuizException("Failed inserting quiz in to database"); 
      } 
      else 
      { 
       // Success inserting quiz, return true 
       return true; 
      } 
     } 
     catch (Exception $insert_exception) 
     { 
      // Error inserting question 
      throw new InsertQuizException(
       "Failed inserting quiz in to database " . 
       $create_quiz_exception->getMessage() 
      ); 
     } 
    } 
} 

實際上,如果不能正確地插入記錄,你拋出一個異常插入。如果代碼塊出現任何問題,您可以抓住它並再次拋出一個Insert異常。創建功能(或其他可能的功能)也一樣。

在代碼的主塊,你可以有:

try 
{ 
    // Create a blank new quiz maker object 
    $quiz_object = new QuizMaker(); 

    // Set the quiz non-question variables 
    $quiz_object->quiz_name = $_POST['quiz_name']; 
    $quiz_object->quiz_intro = $_POST['quiz_intro']; 
      //... and so on ... 

    // Create the quiz record in the database if it is not already set 
    $quiz_object->CreateQuizRecord(); 

    // Insert the quiz in to the database 
    $quiz_object->InsertQuestions(); 

} 
catch (InsertQuizException $insert_exception) 
{ 
    // Insert error 
} 
catch (CreateQuizException $create_quiz_exception) 
{ 
    // Create error 
} 
catch (Exception $quiz_maker_error) 
{ 
    // Any other error 
} 

如果你不希望有多個catch塊存在,只要保持趕上(例外)一個,然後檢查它的類型拋出每個異常以指定從那裏採取的操作。

HTH

相關問題