Php Error Handling Vs. Exception Handling
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 6.2 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Difference between exceptions and errors? up vote 4 down vote favorite 1 What is the difference between an error and an exception? I have read numerous resources online and in a couple of books, but the explanations provided are not very thorough. As such, I am still confused. Thanks! Edit: It looks like I asked two questions which was probably confusing. The main question that I wanted an answer to is the difference between errors and exceptions. So, I have edited the above to be more specific. Thanks everybody for your answers. php exception-handling error-handling share|improve this question edited May 27 '10 at 22:50 asked May 27 '10 at 17:50 Mike Moore 1,95352851 On a side note, you should use exceptions only for exceptional situations. Invalid user input happens all the time and should be expected. –Lotus Notes May 27 '10 at 18:09 Duplicate. Should be merged with stackoverflow.com/questions/841500/php-exceptions-vs-errors –Kenaniah May 27 '10 at 18:17 add a comment| 3 Answers 3 active oldest votes up vote 5 down vote accepted There is no "should" or "best" way to do error handling. Generally speaking, there are two types of errors Those which are handled by other parts of the program. The user never sees or knows about these errors, at least not in a direct manner. Those which have caused enough failure that the user needs to be informed as such. Notice that neither of these have anything to do with the specific PHP mechanisms you'd use to handle errors. If you use exceptions... Then I recommend using exceptions across the board. Register an exception handler and let it do most of the work - including other PHP errors. Invalid login details? class InvalidLoginException extends Exception { protected $message = 'Login information is incorrect. Please try again.'; } Then you have a bunch of choices for implementation. try { $user->login(); // will throw and Invali
and Objects Namespaces Errors Exceptions Generators References Explained Predefined Variables Predefined Exceptions Predefined Interfaces and Classes Context options and parameters Supported Protocols and Wrappers Security Introduction General considerations Installed as CGI binary Installed as an Apache module Session Security Filesystem Security Database Security Error Reporting Using Register Globals User Submitted Data Magic Quotes Hiding PHP Keeping Current Features HTTP authentication with PHP Cookies Sessions Dealing with XForms Handling file uploads Using remote files Connection handling Persistent Database Connections Safe Mode Command line usage Garbage Collection DTrace Dynamic Tracing Function Reference Affecting PHP's Behaviour Audio Formats Manipulation Authentication http://stackoverflow.com/questions/2923514/difference-between-exceptions-and-errors Services Command Line Specific Extensions Compression and Archive Extensions Credit Card Processing Cryptography Extensions Database Extensions Date and Time Related Extensions File System Related Extensions Human Language and Character Encoding Support Image Processing and Generation Mail Related Extensions Mathematical Extensions Non-Text MIME Output Process Control Extensions Other Basic Extensions Other Services Search Engine Extensions Server Specific Extensions Session Extensions Text Processing Variable and Type http://php.net/manual/en/class.errorexception.php Related Extensions Web Services Windows Only Extensions XML Manipulation Keyboard Shortcuts? This help j Next menu item k Previous menu item g p Previous man page g n Next man page G Scroll to bottom g g Scroll to top g h Goto homepage g s Goto search(current page) / Focus search box ErrorException::__construct » « Exception::__clone PHP Manual Language Reference Predefined Exceptions Change language: English Brazilian Portuguese Chinese (Simplified) French German Japanese Korean Romanian Russian Spanish Turkish Other Edit Report a Bug ErrorException (PHP 5 >= 5.1.0, PHP 7) Introduction An Error Exception. Class synopsis ErrorException extends Exception { /* Properties */ protected int $severity ; /* Inherited properties */ protected string $message ; protected int $code ; protected string $file ; protected int $line ; /* Methods */ public __construct ([ string $message = "" [, int $code = 0 [, int $severity = E_ERROR [, string $filename = __FILE__ [, int $lineno = __LINE__ [, Exception $previous = NULL ]]]]]] ) final public int getSeverity ( void ) /* Inherited methods */ final public string Exception::getMessage ( void ) final public Exception Exception::getPrevious ( v
Learn Bootstrap Learn Graphics Learn Icons Learn How To JavaScript Learn JavaScript Learn jQuery Learn jQueryMobile Learn AppML http://www.w3schools.com/php/php_exception.asp Learn AngularJS Learn JSON Learn AJAX Server Side Learn SQL https://wiki.haskell.org/Error_vs._Exception Learn PHP Learn ASP Web Building Web Templates Web Statistics Web Certificates XML Learn XML Learn XML AJAX Learn XML DOM Learn XML DTD Learn XML Schema Learn XSLT Learn XPath Learn XQuery × HTML HTML Tag Reference HTML Event Reference HTML php error Color Reference HTML Attribute Reference HTML Canvas Reference HTML SVG Reference Google Maps Reference CSS CSS Reference CSS Selector Reference W3.CSS Reference Bootstrap Reference Icon Reference JavaScript JavaScript Reference HTML DOM Reference jQuery Reference jQuery Mobile Reference AngularJS Reference XML XML Reference XML Http Reference XSLT Reference XML Schema Reference Charsets HTML php error handling Character Sets HTML ASCII HTML ANSI HTML Windows-1252 HTML ISO-8859-1 HTML Symbols HTML UTF-8 Server Side PHP Reference SQL Reference ASP Reference × HTML/CSS HTML Examples CSS Examples W3.CSS Examples Bootstrap Examples JavaScript JavaScript Examples HTML DOM Examples jQuery Examples jQuery Mobile Examples AngularJS Examples AJAX Examples XML XML Examples XSLT Examples XPath Examples XML Schema Examples SVG Examples Server Side PHP Examples ASP Examples Quizzes HTML Quiz CSS Quiz JavaScript Quiz Bootstrap Quiz jQuery Quiz PHP Quiz SQL Quiz XML Quiz × PHP Tutorial PHP HOME PHP Intro PHP Install PHP Syntax PHP Variables PHP Echo / Print PHP Data Types PHP Strings PHP Constants PHP Operators PHP If...Else...Elseif PHP Switch PHP While Loops PHP For Loops PHP Functions PHP Arrays PHP Sorting Arrays PHP Superglobals PHP Forms PHP Form Handling PHP Form Validation PHP Form Required PHP Form URL/E-mail PHP Form Complete PHP Advanced PHP Arrays Multi PHP Date and Time PHP
in Haskell-Cafe and more and more packages that handle errors and exceptions or something between. Although both terms are related and sometimes hard to distinguish, it is important to do it carefully. This is like the confusion between parallelism and concurrency. The first problem is that "exception" seems to me to be the historically younger term. Before there were only "errors", independent of whether they were programming, I/O or user errors. In this article we use the term exception for expected but irregular situations at runtime and the term error for mistakes in the running program that can be resolved only by fixing the program. We do not want to distinguish between different ways of representing exceptions: Maybe, Either, exceptions in IO monad, or return codes, they all represent exceptions and are worth considering for exception handling. The history may have led to the identifiers we find today in the Haskell language and standard Haskell modules. Exceptions: Prelude.catch, Control.Exception.catch, Control.Exception.try, IOError, Control.Monad.Error Errors: error, assert, Control.Exception.catch, Debug.Trace.trace Note, that the catch function from Prelude handles exclusively exceptions, whereas its counterpart from Control.Exception also catches certain kinds of undefined values. Prelude> catch (error "bla") (\msg -> putStrLn $ "caught " ++ show msg) *** Exception: bla Prelude> Control.Exception.catch (error "bla") (\msg -> putStrLn $ "caught " ++ show (msg::Control.Exception.SomeException)) caught bla This is unsafe, since Haskell's error is just sugar for undefined, that shall help spotting a programming error. A program should work as well when all errors and undefineds are replaced by infinite loops. However infinite loops in general cannot be caught, whereas calls to sugared functions like error can. Even more confusion was initiated by the Java programming language to use the term "exceptions" for programming errors like the NullPointerException and introducing the distinction between checked and unchecked exceptions. Contents 1 Examples 2 When exceptions become errors 3 When errors become exc