2012-09-06 43 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

相关问题