Exception::getMessage: The getMessage exception in PHP language is basically used by the programmers to know the Exception message. It means that whenever an exception condition occurs in a code, in order to know the exact meaning and what that exception is all about this function is been used. This function is quite useful for the programmer because it helps him to find the true nature of that exception and using this valuable information he/she can write the correct exception handling code.
Example: In the below code, the getMessage() will get the exception message.
PHP
<?php try { throw new Exception( " error message" ); } catch (Exception $e ) { echo $e ->getMessage(); } ?> |
error message
Exception::getLine: The getLine exception in PHP language is basically used by the programmers in order to know at which line the corresponding exception has occurred. It means that whenever an exception occurs in a code, this particular getLine() function can find out the exact position of the code where this exception has occurred. This functionality helps at times where we have a huge code and we are unable to find out the location of a particular exception.
Example: In the below code, the getLine() function will get the line at which the exception has occurred.
PHP
<?php try { throw new Exception( " error message" ); } catch (Exception $e ) { echo "The exception has occurred on line: " . $e ->getLine(); } ?> |
The exception has occurred on line: 3
Difference between Exception::getMessage and Exception::getLine :
Exception::getMessage | Exception::getLine |
This function returns the exception message. | This function returns the position of the line at which the exception has occurred. |
It returns the exception message in string format. | It returns the line number in integer format. |
It is helpful in all types of codes. | It is most helpful in huge codes ie. Codes containing many lines. |