Guest User

Untitled

a guest
May 21st, 2018
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.93 KB | None | 0 0
  1. $foo = bar() or die('Error: bar function return false.');
  2.  
  3. mysql_query('SELECT ...') or die('Error in during the query');
  4.  
  5. try{
  6. $foo = bar() or throw new Exception('We have a problem here');
  7.  
  8. $foo = bar() or function(){ throw new Exception('We have a problem here'); }
  9.  
  10. function ThrowMe($mess, $code){
  11. throw new Exception($mess, $code);
  12. }
  13. try{
  14. $foo = bar() or ThrowMe('We have a problem in here', 666);
  15. }catch(Exception $e){
  16. echo $e->getMessage();
  17. }
  18.  
  19. try{
  20. $foo = bar();
  21. if(!$foo){
  22. throw new Exception('We have a problem in here');
  23. }
  24. }catch(Exception $e){
  25. echo $e->getMessage();
  26. }
  27.  
  28. #The echo $e->getMessage(); is just an example, in real life this have no sense!
  29. try{
  30. $foo = bar();
  31. if(!$foo){
  32. throw new Exception('Problems with bar()');
  33. }
  34. $aa = bb($foo);
  35. if(!$aa){
  36. throw new Exception('Problems with bb()');
  37. }
  38. //...and so on!
  39. }catch(Exception $e){
  40. echo $e->getMessage();
  41. }
  42.  
  43. #But i relly prefer to use something like:
  44.  
  45. try{
  46. $foo = bar() or throw new Exception('Problems with bar()');
  47. $aa = bb($foo) or throw new Exception('Problems with bb()');
  48. //...and so on!
  49. }catch(Exception $e){
  50. echo $e->getMessage();
  51. }
  52.  
  53. #Actually, the only way i figured out is:
  54.  
  55. try{
  56. $foo = bar() or throw new ThrowMe('Problems with bar()', 1);
  57. $aa = bb($foo) or throw new ThrowMe('Problems with bb()', 2);
  58. //...and so on!
  59. }catch(Exception $e){
  60. echo $e->getMessage();
  61. }
  62.  
  63. #But i'll love to thro the exception directly instead of trick it with ThrowMe function.
  64.  
  65. mysql_query() or die();
  66.  
  67. mysql_query() || die();
  68.  
  69. bar() or throw new Exception();
  70.  
  71. (boolean)throw new Exception();
  72.  
  73. function foo() {
  74. try {
  75. $bar = bar();
  76. } catch (BarException) {
  77. throw new FooException;
  78. }
  79. }
  80.  
  81. $foo = bar();
  82. if(!$foo){
  83. echo 'We have a problem in here';
  84. }
  85.  
  86. $foo = bar();
  87. if(!$foo){
  88. throw new Exception('We have a problem in here');
  89. }
Add Comment
Please, Sign In to add comment