Home php Catch block does not work (PHP, Try Catch design)

Catch block does not work (PHP, Try Catch design)

Author

Date

Category

I certainly have an elementary question, but I can’t understand the principle of work of the Try-Catch design on PHP.

In general, we have the most simple code as below.
The function checknum ($ number) checks whether the number is greater than or less than 5.

The function is performed in the TRY unit.

If in the TRY block we will do some error (for example, we call the checknnnums function, which does not exist), then this in theory should lead to the exception and execution of the Catch unit.

really appears an error, but the code that in the Catch block does not work, i.e. For example, we do not see the inscriptions laid in it ‘worked the Catch block – Message:’, but just get
Fatal Error: Uncaught Error: Call to undefined Function Checknums ()

I need to work exactly try-catch, I understand that there are options for solving through alternative options, but it is necessary that way.

PHP 7.3

Code below:

& lt;? php
FUNCTION CHECKNUM ($ NUMBER) {
  if ($ Number & gt; 5) {
   Echo "Number more than 5";
  } else {
   Echo "Number less than 5";
  }
}
Try {
 Checknum (7);
 Echo 'worked the Try' block;
}
Catch (Exception $ E) {
 Echo 'worked the Catch block - Message:'. $ E- & GT; GetMessage ();
}
? & gt;

Answer 1, Authority 100%

If in the TRY block we will do some error (for example, let’s call the function checknnnums, which does not exist), then this in theory should lead to the exception of

is completely true for your PHP version. (Not sure of his memory and quickly verified, probably for all PHP 7.0 and newer)

Your error and the error is that you expect to generate the exclusion of the Exception . Perhaps you think that all exceptions are inherited from Exception . And just that is incorrect. An exception in this case is generated by the class error and this class in its inheritance tree does not relieve on Exception . The Inheritance Tree in PHP7.0 and the newer looks like this :

throwable
 Error
  ARITHMETICERROR.
   DivisionByzeroerror
  Assertionerror
  CompileError.
   Parseerror
  Typeerror
   ArgumentCounterror
 Exception

And if you replace your Catch on:

catch (\ throwable $ E) {

It will already catch this error in your PHP version.


Nevertheless, note that not all PHP errors are replaced by exclusion generation. The mechanism of errors and exceptions are not connected with each other.


Answer 2, Authority 17%

To catch a mistake, you need to throw it through Throw New Exception ();

& lt;? php
FUNCTION CHECKNUM ($ NUMBER) {
  if ($ Number & gt; 5) {
   Echo "Number more than 5". Php_eol;
  } else {
   Echo "Number less than 5". Php_eol;
  }
}
$ ValidFunctionName = 'Checknum';
$ invalidfunctionname = 'checknum1';
// successfully completed
Try {
  if (! Function_exists ($ validfunctionName))
    Throw New Exception ('method not found.');
  $ ValidFunctionName (7);
  Echo 'worked the TRY' block. Php_eol;
} Catch (Exception $ E) {
 Echo 'worked the Catch block - Message:'. $ E- & GT; GetMessage ();
}
// Ensure Exception
Try {
  if (! Function_exists ($ InvalidFunctionName))
    Throw New Exception ('method not found.');
  $ InvalidFunctionName (7);
  Echo 'worked the TRY' block. Php_eol;
} Catch (Exception $ E) {
 Echo 'worked the Catch block - Message:'. $ E- & GT; GetMessage ();
}

Conclusion:

number more than 5
Worked the Try block
Catch Catch - Message: The method is not found.

If you need to throw out an exception in the method, you can use the following design:

& lt;? php
FUNCTION CHECKNUM ($ NUMBER) {
   if ($ Number & gt; 5) {
    Echo "Number more than 5". Php_eol;
   } else {
     Throw New Exception ('number less than 5');
   }
}
Try {
   if (! function_exists ('Checknum'))
     Throw New Exception ('method not found.');
   Checknum (7);
   Echo 'consider Checknum with an argument 7'. Php_eol;
   Checknum (3);
   Echo 'consider checknum with 3' argument. Php_eol;
} Catch (Exception $ E) {
  Echo 'worked the Catch block - Message:'. $ E- & GT; GetMessage ();
}

Conclusion:

number more than 5
We consider checknum at argument 7
Catch Catch - Message: Number Less 5

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions