Home

getMessage

getMessage is a common method name used in several programming languages to retrieve the textual description associated with an object, most often an exception or error object. It returns the detail message that explains what went wrong or what the object represents.

In Java, getMessage is defined by the Throwable class. Its signature is public String getMessage(). It returns

In PHP, getMessage is a method of the Exception class (and its subclasses). It returns a string

Other languages may expose similar functionality under different names. For instance, C# uses a Message property

Usage of getMessage typically occurs after catching an exception to log or display a descriptive error, or

See also: getLocalizedMessage in Java, exception handling, logging practices.

the
detail
message
string
that
was
provided
when
the
throwable
was
created,
or
null
if
no
message
was
given.
Java
also
offers
getLocalizedMessage()
for
locale-specific
formatting
of
the
message.
containing
the
exception
message
that
was
set
when
the
exception
was
constructed,
for
example,
new
Exception("Invalid
input");
then
$e->getMessage()
yields
"Invalid
input".
If
no
message
was
provided,
the
method
returns
an
empty
string.
on
System.Exception
rather
than
a
getMessage
method,
while
Ruby
exposes
a
message
method.
In
many
custom
classes,
getMessage
may
be
implemented
as
part
of
a
standard
interface
or
convention
for
retrieving
descriptive
text.
to
construct
user-facing
feedback
without
exposing
sensitive
internal
details.
When
presenting
messages,
developers
should
avoid
leaking
sensitive
information
and
consider
localization
or
abstraction
of
error
details.