• 13
name

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 191

Backtrace:

File: /home/prodcxja/public_html/questions/application/views/question.php
Line: 191
Function: _error_handler

File: /home/prodcxja/public_html/questions/application/controllers/Questions.php
Line: 433
Function: view

File: /home/prodcxja/public_html/questions/index.php
Line: 315
Function: require_once

name Punditsdkoslkdosdkoskdo

LogicException vs. RuntimeException

I was wondering if there was a rule saying when to use which Exception in PHP... When do I have to throw a LogicException and when a RuntimeException?

For example when it comes to exceptions like PageNotFoundException, from which exception class should I inherit?

LogicException seems like it's for "this can never happen" bug checks:

Exception that represents error in the program logic. This kind of exception should directly lead to a fix in your code.

A few of the other SPL exceptions, like BadFunctionCallException inherit from it.

RuntimeException is for cases where an error happens that could only be detected while the program is running. The naming is a holdover from compiled languages, where certain errors can be detected at compile time. Like LogicException, a few of the other SPL exceptions inherit from it.

You probably don't want to use either of these as the base for your own specific extensions unless you know for sure that your code could produce another exception in the inheritance hierarchy and you'd want to catch any of those instead of your specific exception or all exceptions.

  • 32
Reply Report

Warm tip !!!

This article is reproduced from Stack Exchange / Stack Overflow, please click

Trending Tags

Related Questions