Download presentation
Presentation is loading. Please wait.
Published byKevin Jenkins Modified over 8 years ago
1
Martin Kruliš Who is General Failure and why is he reading my disk? 19. 3. 2015 by Martin Kruliš (v1.0)1
2
Concept of Exceptions ◦ Similar to other object languages Exceptions are thrown and caught ( throw, catch ) Exception is an object of class Exception or derived class ◦ Used for reporting errors Especially from deeply nested code ◦ Note that … Uncaught exception causes Fatal Error Destructors should not throw exceptions Throwing-catching process is somewhat slow 19. 3. 2015 by Martin Kruliš (v1.0)2
3
Try-catch Blocks ◦ Exception-safe code is wrapped in try block ◦ First matching catch block handles the exception Exception is matched by its class try {... Code that may throw an exception... } catch (MyException $e) {... My exception handler... } catch (Exception $e) {... Generic exception handler... } 19. 3. 2015 by Martin Kruliš (v1.0)3
4
Throwing Exceptions ◦ Keyword throw followed by the exception object throw new Exception('Error …'); ◦ It can be used inside catch-block to re-throw already caught exception try {... } catch (MyException $e) {... Partial exception handling... throw $e; // re-throwing exception } 19. 3. 2015 by Martin Kruliš (v1.0)4
5
Creating Custom Exceptions ◦ Exception class is derived from Exception ◦ Derived classes need not to override any methods If the constructor is overridden, parent constructor must be invoked It is recommended to redefine __toString() ◦ When to customize… To distinguish a new error type To add additional data to the exception object 19. 3. 2015 by Martin Kruliš (v1.0)5
6
Internal Errors ◦ Should not be displayed to the user User cannot understand them, nor counteract them Potential security breach ◦ Save them for admin (log, e-mail, …) Error Control Operator ◦ Symbol @ prepended to an expression ◦ All error messages from the expression are ignored ◦ Suitable for specific local solutions only $data = @file('data_file.txt') or die('Error…'); 19. 3. 2015 by Martin Kruliš (v1.0)6
7
Error Levels ◦ Define the severity of the error E_ERROR – fatal errors, terminate the script E_WARNING – severe errors, but recoverable E_NOTICE – unusual situations (possible error) E_USER_xxx – user level error, warning, or notice E_STRICT – suggestion for improvement ◦ The log can filter selected error levels Controlled in php.ini or by error_reporting() ◦ User errors can be triggered manually trigger_error() 19. 3. 2015 by Martin Kruliš (v1.0)7
8
Setting Up Error Handling ◦ set_error_handler() Sets a callback that handles runtime errors Except for Fatal Errors Can yield handling back to default control By returning false from the callback function Default control acts according to php.ini settings ◦ set_exception_handler() Sets generic handler for uncaught exceptions ◦ restore_error_handler(), restore_exception_handler() Restore original error handling control 19. 3. 2015 by Martin Kruliš (v1.0)8
9
Information about Errors ◦ The handling callback gets error level and position ◦ debug_backtrace() – retrieves current call stack ◦ debug_print_backtrace() – prints call stack ◦ Might be wise to log $_GET, $_POST, $_COOKIES, … PHP Supported Error Logging ◦ error_log() – logs error internally ◦ Error message can be written to the web server log, sent as e-mail, or written to a file or a TCP channel 19. 3. 2015 by Martin Kruliš (v1.0)9 Example
10
19. 3. 2015 by Martin Kruliš (v1.0)10
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.