Shafizadeh Shafizadeh - 1 year ago 66
PHP Question

Can I use throw without any message?

Here is my code:

try {

if ( condition 1 ) {
} else {
// do something

// some code here

if ( condition 2 ){

} catch (Exception $e) {
echo "something is wrong";

As you see, my
block has its own error message, And that message is a constant. So really I don't need to pass a message when I use
like this:

throw new Exception('error message');

Well can I use
without anything? I just need to jump into

Honestly writing an useless error message is annoying for me.

As you know my current code has a syntax error: (it referring to

Parse error: syntax error, unexpected ';' in {path}

Answer Source

message parameter is optional in the Exception constructor. So if you don't have/want to put - just don't:

throw new Exception;

But you still must throw an instance of the Exception class (or a class that extends it), since it is a part of the php language syntax.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download