Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Difference between Exception::getMessage and Exception::getLine

  • Last Updated : 31 Oct, 2021

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();
    }
?>
Output
 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 occured on line: " 
             . $e->getLine();
    }
?>
Output
The exception has occured on line: 3

Difference between Exception::getMessage and Exception::getLine :

Exception::getMessageException::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.


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!