Advertisement
Achilles

php try and catch

Jul 19th, 2013
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.46 KB | None | 0 0
  1. <?php
  2.  
  3. class InputException extends Exception
  4. {
  5. private $_invalidWarpFactor;
  6.  
  7. public function __construct( $message , $code, $factor )
  8. {
  9. parent::__construct( $message , $code );
  10. $this->_invalidWarpFactor = $factor;
  11. }
  12.  
  13. public function getInvalidWarpFactor()
  14. {
  15. return $this->_invalidWarpFactor;
  16. }
  17. } // End of class Exception
  18.  
  19.  
  20. class FuelException extends Exception
  21. {
  22. private $_remainingFuel;
  23.  
  24. public function __construct( $message , $code , $remainingFuel )
  25. {
  26. parent::__construct( $message , $code );
  27. $this->_remainingFuel = $remainingFuel;
  28. }
  29.  
  30. public function getRemainingFuel()
  31. {
  32. return $this->_remainingFuel;
  33. }
  34.  
  35.  
  36. } // End of class FuelException
  37.  
  38.  
  39. class WarpDrive
  40. {
  41. static private $_dilithiumLevel = 10;
  42.  
  43. public function setWarpFactor( $factor )
  44. {
  45. if( $factor < 1 )
  46. {
  47. throw new InputException( "Warp need to be atleast 1", 1 , $factor );
  48. }
  49.  
  50. else if( $factor > 9 )
  51. {
  52. throw new InputException( "Warp Factor exceeds drive specification" , 2 , $factor );
  53. }
  54.  
  55. else if( WarpDrive::$_dilithiumLevel < $factor )
  56. {
  57. throw new FuelException( "Insufficient fuel" , 3 , WarpDrive::$_dilithiumLevel );
  58. }
  59.  
  60. else
  61. {
  62. WarpDrive::$_dilithiumLevel -= $factor;
  63. echo "<p>Now traveling at a warp factor $factor</p>";
  64. }
  65. }
  66. } // End of class WarpDrive
  67.  
  68. class ChiefEngineer
  69. {
  70. public function doWarp( $factor )
  71. {
  72. $wd = new WarpDrive();
  73. $wd->setWarpFactor($factor);
  74. }
  75. }
  76.  
  77. class Captain
  78. {
  79. public function newWarpOrder( $factor )
  80. {
  81. $ce = new ChiefEngineer();
  82.  
  83. try
  84. {
  85. $ce->doWarp( $factor );
  86. }
  87.  
  88. catch( InputException $e )
  89. {
  90. echo "<p>Captain's log: Warp factor " . $e->getInvalidWarpFactor() . "? I must be losing my mind ...</p>";
  91. }
  92.  
  93. catch( FuelException $e )
  94. {
  95. echo "<p>Captain log: I'm getting fuel problem from the warp engine. It says '" . $e->getMessage();
  96. echo "' . we have " . $e->getRemainingFuel() . "dilithium left. I guess w're not going anywhere</p>";
  97. }
  98.  
  99. catch( Exception $e )
  100. {
  101. echo "<p>Captain's log: something else happend , I don't know what. The message is '" . $e->getMessage() ."'<p>";
  102. }
  103.  
  104. }
  105. } // End of class Captain
  106.  
  107. $c = new Captain();
  108. $c->newWarpOrder(5);
  109. $c->newWarpOrder(-1);
  110. $c->newWarpOrder(12);
  111. $c->newWarpOrder(4);
  112. $c->newWarpOrder(9);
  113.  
  114.  
  115. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement