Search code examples
laravellaravel-8laravel-9

Difference between Exception and QueryException in Laravel


What is the difference between Exception and Illuminate\Database\QueryException in Laravel when I use it in try catch.


Solution

  • Exception is from php, QueryException is from laravel and extends PDOException from php, which in turn also extends Exception, so all exceptions class are childrens of Exception somehow

    Exception is very generic

    So if you have a QueryException, it's related to eloquent and a query you have made, it's more specific than Exception which can be anything at all

    What QueryException does it's just to add the (SQL: ...) to the exception message

    When you use a try catch, Exception will catch anything, including QueryException, while QueryException will only catch QueryException, and will not catch EntryNotFoundException for example, or any other exception from laravel or php

    If you catch PDOException, QueryException will also be catched because QueryException extends PDOException