How to throw an exception in PHP?

by tressie.damore , in category: PHP General , 2 years ago

How to throw an exception in PHP?

Facebook Twitter LinkedIn Telegram Whatsapp

2 answers

Member

by adan , 2 years ago

@tressie.damore 

In PHP, you can throw an exception using the throw keyword followed by an instance of the Exception class or one of its subclasses. Here's an example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
function divide($dividend, $divisor) {
    if ($divisor == 0) {
        throw new Exception("Division by zero");
    }
    return $dividend / $divisor;
}

// Example usage:
try {
    $result = divide(10, 0);
    echo "Result: $result";
} catch (Exception $e) {
    echo "Caught exception: " . $e->getMessage();
}


In this example, the divide function checks whether the divisor is zero, and if so, it throws a new Exception with the message "Division by zero". The try block attempts to call divide(10, 0), which throws an exception, and the catch block catches the exception and prints its message.


You can also define your own exception classes by extending the Exception class or one of its subclasses. For example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
class MyException extends Exception {
    // custom implementation
}

function foo() {
    throw new MyException("Something went wrong");
}

try {
    foo();
} catch (MyException $e) {
    echo "Caught MyException: " . $e->getMessage();
} catch (Exception $e) {
    echo "Caught Exception: " . $e->getMessage();
}


In this example, MyException extends Exception, and the foo function throws a new MyException with the message "Something went wrong". The try block attempts to call foo(), which throws a MyException, and the catch block catches the MyException and prints its message. If another type of exception is thrown, the second catch block catches it and prints its message instead.

Member

by dana , a year ago

@tressie.damore 

To throw an exception in PHP, you can use the throw keyword followed by an instance of the Exception class or a subclass of it. Here's an example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
function divide($dividend, $divisor) {
    if ($divisor == 0) {
        throw new Exception("Division by zero");
    }
    return $dividend / $divisor;
}

try {
    $result = divide(10, 0);
    echo "Result: $result";
} catch (Exception $e) {
    echo "Caught exception: " . $e->getMessage();
}


In this example, the divide function checks if the divisor is zero, and if so, it throws a new instance of the Exception class with the message "Division by zero". The try block attempts to call divide(10, 0), which throws an exception. The catch block catches the exception and prints its message.


You can create your custom exception classes by extending the Exception class or any of its subclasses. Here's an example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
class MyException extends Exception {
    // custom implementation
}

function foo() {
    throw new MyException("Something went wrong");
}

try {
    foo();
} catch (MyException $e) {
    echo "Caught MyException: " . $e->getMessage();
} catch (Exception $e) {
    echo "Caught Exception: " . $e->getMessage();
}


In this example, MyException extends Exception, and the foo function throws a new MyException instance with the message "Something went wrong". The try block attempts to call foo(), which throws a MyException, and the first catch block catches the MyException and prints its message. If another type of exception is thrown, the second catch block catches it and prints its message instead.