SlideShare a Scribd company logo
1 of 17
Introduction
• How to write an error handler.
• Normally, it displays a message indicating the cause of the error and may
also terminate script execution when a PHP script encounters an error.
• Now, while this behavior is acceptable during the development phase, it
cannot continue once a PHP application has been released to actual users.
• In "live" situations, it is unprofessional to display cryptic error messages
(which are usually incomprehensible to non-technical users).
• It is more professional to intercept these errors and either resolve them (if
resolution is possible), or notify the user with an easily-understood error
message (if not).
3/4/2020 1
Types of runtime errors in PHP
• There are 3 basic types of runtime errors in PHP:
i. Notices: These are trivial, non-critical errors that PHP encounters while
executing a script. By default, such errors are not displayed to the user at
all – we can change this default behavior.
ii. Warnings: These are more serious errors - for example, attempting to
include() a file which does not exist. By default, these errors are
displayed to the user, but they do not result in script termination.
iii. Fatal errors: These are critical errors - for example, instantiating an
object of a non-existent class, or calling a non-existent function. These
errors cause the immediate termination of the script, and PHP's default
behavior is to display them to the user when they take place.
3/4/2020 2
Cont…
• It should be noted that a syntax error in a PHP script – (EG: missing brace or semi-
colon) is treated as a fatal error and results in script termination (stop execution).
• PHP errors can be generated by:
i. Zend engine
ii. PHP built-in functions
iii. user-defined functions
They may occur at startup, at parse-time, at compile-time or at run-time.
• During the debug phase:
Use E_ALL type to see all fatal and non-fatal errors generated by our script.
Use the new E_STRICT error type to view errors that affect the forward
compatibility of our code in PHP 5.
3/4/2020 3
Early Warning
• Example 1
 we get a non-fatal error (E_WARNING)
it still get executed but with non-fatal error
• Example 2
it call non-existent function
generate a fatal error immediately stops script execution
3/4/2020 4
Example 1
3/4/2020 5
Example 2
3/4/2020 6
Rolling Your Own
• Changing the way errors are handled.
• Function called set_error_handler(), it allows to divert all PHP errors to a custom
function that are defined, instead of sending them to the default handler.
• This custom function must be capable of accepting a minimum of two mandatory
arguments:
i. error type
ii. corresponding descriptive message
• And up to three additional arguments
i. the file name
ii. line number where the error occurred
iii. dump of the variable space at the time of error
3/4/2020 7
Example 3
The set_error_handler() function tells the script that all errors are to be
routed to my user-defined oops() function
This function is set up to accept five arguments:
i. error type
ii. message
iii. file name
iv. line number
v. context
These arguments are then used to create an error page that is friendlier
and more informative than PHP's standard one-line error message
3/4/2020 8
Cont…
3/4/2020 9
Cont..
3/4/2020 10
Cont…
• An error occurred while executing this script. Please contact the
webmaster to report this error.Here is the information provided by the
script:
• Error code: 2
Error message: Wrong parameter count for explode() Script name and
line number of error: C:wampwwwtest2part12eg5.php:12 Variable
state when error occurred: a string
3/4/2020 11
Pulling the Trigger
• PHP allows you to use its built-in error handling system to raise your
own custom errors as well.
• This is accomplished via a function named trigger_error(), which
allows you to raise any of the three error types reserved for users:
E_USER_NOTICE, E_USER_WARNING and E_USER_ERROR.
• When these errors are triggered, PHP's built-in handler will
automatically wake up to handle them.
• Example(triggerror6.php):
3/4/2020 12
Example(triggerror4.php):
3/4/2020 13
Catching up
• In the exception-based approach, program code is wrapped in a try()
block, and exceptions generated by it are "caught" and resolved by a
catch() block.
• Multiple catch() blocks are possible, each one dealing with a different
error type; this allows developers to trap different types of errors and
execute appropriate exceptionhandling.
• When PHP encounters code wrapped within a try-catch() block:
– It first attempts to execute the code within the try() block.
– If this code is processed without any exceptions being generated,
control transfers to the lines following the trycatch() block.
3/4/2020 14
Catching up (cont..)
• The exceptions themselves are generated via PHP's throw statement.
• The throw statement needs to be passed a descriptive message, and
an optional error code.
• When the exception is raised, this description and code will be made
available to the exception handler.
• Example(tryncatcherror4.php):
3/4/2020 15
Example (tryncatcherror4.php):
3/4/2020 16
End of presentation
3/4/2020 17

More Related Content

What's hot

What is cms_in_php
What is cms_in_phpWhat is cms_in_php
What is cms_in_phpSwati Sharma
 
32 OpenMP Traps For C++ Developers
32 OpenMP Traps For C++ Developers32 OpenMP Traps For C++ Developers
32 OpenMP Traps For C++ DevelopersPVS-Studio
 
Exception handling in Python
Exception handling in PythonException handling in Python
Exception handling in PythonAdnan Siddiqi
 
Python Exception Handling
Python Exception HandlingPython Exception Handling
Python Exception HandlingMegha V
 
05 laboratory exercise 2
05 laboratory exercise 205 laboratory exercise 2
05 laboratory exercise 2Anne Lee
 
Why Students Need the CppCat Code Analyzer
Why Students Need the CppCat Code AnalyzerWhy Students Need the CppCat Code Analyzer
Why Students Need the CppCat Code AnalyzerPVS-Studio
 
Java Exceptions and Exception Handling
 Java  Exceptions and Exception Handling Java  Exceptions and Exception Handling
Java Exceptions and Exception HandlingMaqdamYasir
 
(2) cpp imperative programming_exercises
(2) cpp imperative programming_exercises(2) cpp imperative programming_exercises
(2) cpp imperative programming_exercisesNico Ludwig
 
programming type error
programming type errorprogramming type error
programming type errorWalepak Ubi
 
Static analysis and ROI
Static analysis and ROIStatic analysis and ROI
Static analysis and ROIPVS-Studio
 
Static analysis and ROI
Static analysis and ROIStatic analysis and ROI
Static analysis and ROIAndrey Karpov
 
The PHP Way Of TDD - Think First, Code Later
The PHP Way Of TDD - Think First, Code LaterThe PHP Way Of TDD - Think First, Code Later
The PHP Way Of TDD - Think First, Code LaterHiraq Citra M
 
Types of errors
Types of errorsTypes of errors
Types of errorsRiya Josh
 
Walmyr Filho - Lessons learned as software engineer working at appear.in
Walmyr Filho - Lessons learned as software engineer working at appear.inWalmyr Filho - Lessons learned as software engineer working at appear.in
Walmyr Filho - Lessons learned as software engineer working at appear.inAgile Lietuva
 

What's hot (20)

Java
JavaJava
Java
 
VBscript
VBscriptVBscript
VBscript
 
What is cms_in_php
What is cms_in_phpWhat is cms_in_php
What is cms_in_php
 
32 OpenMP Traps For C++ Developers
32 OpenMP Traps For C++ Developers32 OpenMP Traps For C++ Developers
32 OpenMP Traps For C++ Developers
 
Exception handling in python
Exception handling in pythonException handling in python
Exception handling in python
 
Exception handling in Python
Exception handling in PythonException handling in Python
Exception handling in Python
 
Switch statement
Switch statementSwitch statement
Switch statement
 
Python Exception Handling
Python Exception HandlingPython Exception Handling
Python Exception Handling
 
05 laboratory exercise 2
05 laboratory exercise 205 laboratory exercise 2
05 laboratory exercise 2
 
Why Students Need the CppCat Code Analyzer
Why Students Need the CppCat Code AnalyzerWhy Students Need the CppCat Code Analyzer
Why Students Need the CppCat Code Analyzer
 
Java Exceptions and Exception Handling
 Java  Exceptions and Exception Handling Java  Exceptions and Exception Handling
Java Exceptions and Exception Handling
 
(2) cpp imperative programming_exercises
(2) cpp imperative programming_exercises(2) cpp imperative programming_exercises
(2) cpp imperative programming_exercises
 
programming type error
programming type errorprogramming type error
programming type error
 
Static analysis and ROI
Static analysis and ROIStatic analysis and ROI
Static analysis and ROI
 
Static analysis and ROI
Static analysis and ROIStatic analysis and ROI
Static analysis and ROI
 
Switch case looping
Switch case loopingSwitch case looping
Switch case looping
 
Exception handling in ASP .NET
Exception handling in ASP .NETException handling in ASP .NET
Exception handling in ASP .NET
 
The PHP Way Of TDD - Think First, Code Later
The PHP Way Of TDD - Think First, Code LaterThe PHP Way Of TDD - Think First, Code Later
The PHP Way Of TDD - Think First, Code Later
 
Types of errors
Types of errorsTypes of errors
Types of errors
 
Walmyr Filho - Lessons learned as software engineer working at appear.in
Walmyr Filho - Lessons learned as software engineer working at appear.inWalmyr Filho - Lessons learned as software engineer working at appear.in
Walmyr Filho - Lessons learned as software engineer working at appear.in
 

Similar to Error handling

Error reporting in php
Error reporting in php Error reporting in php
Error reporting in php Mudasir Syed
 
VISUAL_BASIC_LECTURE_NOTE_A_Z_MADE_EASY.pdf
VISUAL_BASIC_LECTURE_NOTE_A_Z_MADE_EASY.pdfVISUAL_BASIC_LECTURE_NOTE_A_Z_MADE_EASY.pdf
VISUAL_BASIC_LECTURE_NOTE_A_Z_MADE_EASY.pdfNALANDACSCCENTRE
 
Compiler design error handling
Compiler design error handlingCompiler design error handling
Compiler design error handlingRohitK71
 
Mule soft meetup__dubai_12_june- Error Handling
Mule soft meetup__dubai_12_june- Error HandlingMule soft meetup__dubai_12_june- Error Handling
Mule soft meetup__dubai_12_june- Error Handlingsatyasekhar123
 
Php Error Handling
Php Error HandlingPhp Error Handling
Php Error Handlingmussawir20
 
UNIT-2-compiler design
UNIT-2-compiler designUNIT-2-compiler design
UNIT-2-compiler designkamaless4
 
Mulesoft meetup slides mumbai_20113019_exception_handling
Mulesoft meetup slides mumbai_20113019_exception_handlingMulesoft meetup slides mumbai_20113019_exception_handling
Mulesoft meetup slides mumbai_20113019_exception_handlingManish Kumar Yadav
 
Mule meetup Hyderabad
Mule meetup HyderabadMule meetup Hyderabad
Mule meetup HyderabadSravan Lingam
 
lecture 15.pptx
lecture 15.pptxlecture 15.pptx
lecture 15.pptxITNet
 
Session10-PHP Misconfiguration
Session10-PHP MisconfigurationSession10-PHP Misconfiguration
Session10-PHP Misconfigurationzakieh alizadeh
 
Exception handling in Mule 4 _Virtual mule soft meetup may_2020
Exception handling in Mule 4 �_Virtual mule soft meetup may_2020Exception handling in Mule 4 �_Virtual mule soft meetup may_2020
Exception handling in Mule 4 _Virtual mule soft meetup may_2020Om Prakash
 
Virtual MuleSoft Meetup may_2020
Virtual MuleSoft Meetup may_2020Virtual MuleSoft Meetup may_2020
Virtual MuleSoft Meetup may_2020Om Prakash
 
Coimbatore Second Mule Meetup on Error Handling in Mule 4
Coimbatore Second Mule Meetup on Error Handling in Mule 4Coimbatore Second Mule Meetup on Error Handling in Mule 4
Coimbatore Second Mule Meetup on Error Handling in Mule 4pqrs1234
 
Error Handling In Mule 4 | MuleSoft Mysore Meetup #10
Error Handling In Mule 4 | MuleSoft Mysore Meetup #10Error Handling In Mule 4 | MuleSoft Mysore Meetup #10
Error Handling In Mule 4 | MuleSoft Mysore Meetup #10MysoreMuleSoftMeetup
 
Building of systems of automatic C/C++ code logging
Building of systems of automatic C/C++ code loggingBuilding of systems of automatic C/C++ code logging
Building of systems of automatic C/C++ code loggingPVS-Studio
 

Similar to Error handling (20)

Error reporting in php
Error reporting in php Error reporting in php
Error reporting in php
 
VISUAL_BASIC_LECTURE_NOTE_A_Z_MADE_EASY.pdf
VISUAL_BASIC_LECTURE_NOTE_A_Z_MADE_EASY.pdfVISUAL_BASIC_LECTURE_NOTE_A_Z_MADE_EASY.pdf
VISUAL_BASIC_LECTURE_NOTE_A_Z_MADE_EASY.pdf
 
PHP - Introduction to PHP Error Handling
PHP -  Introduction to PHP Error HandlingPHP -  Introduction to PHP Error Handling
PHP - Introduction to PHP Error Handling
 
Compiler design error handling
Compiler design error handlingCompiler design error handling
Compiler design error handling
 
Mule soft meetup__dubai_12_june- Error Handling
Mule soft meetup__dubai_12_june- Error HandlingMule soft meetup__dubai_12_june- Error Handling
Mule soft meetup__dubai_12_june- Error Handling
 
Debugging
DebuggingDebugging
Debugging
 
Php Error Handling
Php Error HandlingPhp Error Handling
Php Error Handling
 
Lecture 20-21
Lecture 20-21Lecture 20-21
Lecture 20-21
 
UNIT-2-compiler design
UNIT-2-compiler designUNIT-2-compiler design
UNIT-2-compiler design
 
Mulesoft meetup slides mumbai_20113019_exception_handling
Mulesoft meetup slides mumbai_20113019_exception_handlingMulesoft meetup slides mumbai_20113019_exception_handling
Mulesoft meetup slides mumbai_20113019_exception_handling
 
Mule meetup Hyderabad
Mule meetup HyderabadMule meetup Hyderabad
Mule meetup Hyderabad
 
Online Spanish meetup #1
Online Spanish meetup #1Online Spanish meetup #1
Online Spanish meetup #1
 
lecture 15.pptx
lecture 15.pptxlecture 15.pptx
lecture 15.pptx
 
Session10-PHP Misconfiguration
Session10-PHP MisconfigurationSession10-PHP Misconfiguration
Session10-PHP Misconfiguration
 
Exception handling in Mule 4 _Virtual mule soft meetup may_2020
Exception handling in Mule 4 �_Virtual mule soft meetup may_2020Exception handling in Mule 4 �_Virtual mule soft meetup may_2020
Exception handling in Mule 4 _Virtual mule soft meetup may_2020
 
Virtual MuleSoft Meetup may_2020
Virtual MuleSoft Meetup may_2020Virtual MuleSoft Meetup may_2020
Virtual MuleSoft Meetup may_2020
 
Coimbatore Second Mule Meetup on Error Handling in Mule 4
Coimbatore Second Mule Meetup on Error Handling in Mule 4Coimbatore Second Mule Meetup on Error Handling in Mule 4
Coimbatore Second Mule Meetup on Error Handling in Mule 4
 
Error Handling In Mule 4 | MuleSoft Mysore Meetup #10
Error Handling In Mule 4 | MuleSoft Mysore Meetup #10Error Handling In Mule 4 | MuleSoft Mysore Meetup #10
Error Handling In Mule 4 | MuleSoft Mysore Meetup #10
 
Unit 5 Java
Unit 5 JavaUnit 5 Java
Unit 5 Java
 
Building of systems of automatic C/C++ code logging
Building of systems of automatic C/C++ code loggingBuilding of systems of automatic C/C++ code logging
Building of systems of automatic C/C++ code logging
 

More from Meherul1234

Breadth-first search in alogorithm
Breadth-first search in alogorithmBreadth-first search in alogorithm
Breadth-first search in alogorithmMeherul1234
 
Binary search in data structure
Binary search in data structureBinary search in data structure
Binary search in data structureMeherul1234
 
Recursion in data structure
Recursion in data structureRecursion in data structure
Recursion in data structureMeherul1234
 
Transistor in electronics
Transistor in electronicsTransistor in electronics
Transistor in electronicsMeherul1234
 
The bus interface unit (biu)
The bus interface unit (biu)The bus interface unit (biu)
The bus interface unit (biu)Meherul1234
 
Decision tree in System Design
Decision tree in System DesignDecision tree in System Design
Decision tree in System DesignMeherul1234
 
Analog to analog conversion
Analog to analog conversionAnalog to analog conversion
Analog to analog conversionMeherul1234
 
Programmable Peripheral Interface
Programmable Peripheral InterfaceProgrammable Peripheral Interface
Programmable Peripheral InterfaceMeherul1234
 

More from Meherul1234 (8)

Breadth-first search in alogorithm
Breadth-first search in alogorithmBreadth-first search in alogorithm
Breadth-first search in alogorithm
 
Binary search in data structure
Binary search in data structureBinary search in data structure
Binary search in data structure
 
Recursion in data structure
Recursion in data structureRecursion in data structure
Recursion in data structure
 
Transistor in electronics
Transistor in electronicsTransistor in electronics
Transistor in electronics
 
The bus interface unit (biu)
The bus interface unit (biu)The bus interface unit (biu)
The bus interface unit (biu)
 
Decision tree in System Design
Decision tree in System DesignDecision tree in System Design
Decision tree in System Design
 
Analog to analog conversion
Analog to analog conversionAnalog to analog conversion
Analog to analog conversion
 
Programmable Peripheral Interface
Programmable Peripheral InterfaceProgrammable Peripheral Interface
Programmable Peripheral Interface
 

Recently uploaded

SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpinRaunakKeshri1
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
The byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptxThe byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptxShobhayan Kirtania
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...fonyou31
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room servicediscovermytutordmt
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Celine George
 

Recently uploaded (20)

SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
The byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptxThe byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptx
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room service
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17Advanced Views - Calendar View in Odoo 17
Advanced Views - Calendar View in Odoo 17
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 

Error handling

  • 1. Introduction • How to write an error handler. • Normally, it displays a message indicating the cause of the error and may also terminate script execution when a PHP script encounters an error. • Now, while this behavior is acceptable during the development phase, it cannot continue once a PHP application has been released to actual users. • In "live" situations, it is unprofessional to display cryptic error messages (which are usually incomprehensible to non-technical users). • It is more professional to intercept these errors and either resolve them (if resolution is possible), or notify the user with an easily-understood error message (if not). 3/4/2020 1
  • 2. Types of runtime errors in PHP • There are 3 basic types of runtime errors in PHP: i. Notices: These are trivial, non-critical errors that PHP encounters while executing a script. By default, such errors are not displayed to the user at all – we can change this default behavior. ii. Warnings: These are more serious errors - for example, attempting to include() a file which does not exist. By default, these errors are displayed to the user, but they do not result in script termination. iii. Fatal errors: These are critical errors - for example, instantiating an object of a non-existent class, or calling a non-existent function. These errors cause the immediate termination of the script, and PHP's default behavior is to display them to the user when they take place. 3/4/2020 2
  • 3. Cont… • It should be noted that a syntax error in a PHP script – (EG: missing brace or semi- colon) is treated as a fatal error and results in script termination (stop execution). • PHP errors can be generated by: i. Zend engine ii. PHP built-in functions iii. user-defined functions They may occur at startup, at parse-time, at compile-time or at run-time. • During the debug phase: Use E_ALL type to see all fatal and non-fatal errors generated by our script. Use the new E_STRICT error type to view errors that affect the forward compatibility of our code in PHP 5. 3/4/2020 3
  • 4. Early Warning • Example 1  we get a non-fatal error (E_WARNING) it still get executed but with non-fatal error • Example 2 it call non-existent function generate a fatal error immediately stops script execution 3/4/2020 4
  • 7. Rolling Your Own • Changing the way errors are handled. • Function called set_error_handler(), it allows to divert all PHP errors to a custom function that are defined, instead of sending them to the default handler. • This custom function must be capable of accepting a minimum of two mandatory arguments: i. error type ii. corresponding descriptive message • And up to three additional arguments i. the file name ii. line number where the error occurred iii. dump of the variable space at the time of error 3/4/2020 7
  • 8. Example 3 The set_error_handler() function tells the script that all errors are to be routed to my user-defined oops() function This function is set up to accept five arguments: i. error type ii. message iii. file name iv. line number v. context These arguments are then used to create an error page that is friendlier and more informative than PHP's standard one-line error message 3/4/2020 8
  • 11. Cont… • An error occurred while executing this script. Please contact the webmaster to report this error.Here is the information provided by the script: • Error code: 2 Error message: Wrong parameter count for explode() Script name and line number of error: C:wampwwwtest2part12eg5.php:12 Variable state when error occurred: a string 3/4/2020 11
  • 12. Pulling the Trigger • PHP allows you to use its built-in error handling system to raise your own custom errors as well. • This is accomplished via a function named trigger_error(), which allows you to raise any of the three error types reserved for users: E_USER_NOTICE, E_USER_WARNING and E_USER_ERROR. • When these errors are triggered, PHP's built-in handler will automatically wake up to handle them. • Example(triggerror6.php): 3/4/2020 12
  • 14. Catching up • In the exception-based approach, program code is wrapped in a try() block, and exceptions generated by it are "caught" and resolved by a catch() block. • Multiple catch() blocks are possible, each one dealing with a different error type; this allows developers to trap different types of errors and execute appropriate exceptionhandling. • When PHP encounters code wrapped within a try-catch() block: – It first attempts to execute the code within the try() block. – If this code is processed without any exceptions being generated, control transfers to the lines following the trycatch() block. 3/4/2020 14
  • 15. Catching up (cont..) • The exceptions themselves are generated via PHP's throw statement. • The throw statement needs to be passed a descriptive message, and an optional error code. • When the exception is raised, this description and code will be made available to the exception handler. • Example(tryncatcherror4.php): 3/4/2020 15