SlideShare a Scribd company logo
1 of 10
Topic: Error and Exception Handling in PHP
Error and Exception Handling in PHP
Contents
• Error and Error Handling
• Using die() function
• Defining Custom Error Handling Function
• Error Parameter
• Possible Error levels
• Possible Error levels Exceptions Handling
• Creating Custom Exception Handler
Page 2 out of 10Error and Exception Handling in PHP
Error and Error Handling
• An error is something you have done which is considered to be incorrect or
wrong, or which should not have been done.
• Error handling is the process of catching errors raised by our program and then
taking appropriate action to prevent unforeseen consequences.
Page 3 out of 10Error and Exception Handling in PHP
Using die() function
By using die() function we can stop the process or jump to the next process if there
is a error. Normally it is used when file is not found.
Example:
Page 4 out of 10
if(!file_exists("/tmp/test.txt")) {
die("File not found"); }
else
{ $file = fopen("/tmp/test.txt","r");
print "Opend file; }
Error and Exception Handling in PHP
Defining Custom Error Handling Function
• We can write our own function to handling any error. PHP provides us a
framework to define error handling function.
• This function must be able to handle minimum two parameters (error level and
error message) but can accept up to five parameters (optionally: file, line-
number, and the error context)
Syntax:
Error_Function_Name(Error_Level, Error_Message, Error_File, Error_Line, Error_Context)
Page 5 out of 10Error and Exception Handling in PHP
Error Parameter
There are 5 kinds of error parameters
• Error_level - A value number, specifies the error report level for the user-defined
error.
• Error_message - Message for the user-defined error
• Error_file - File name in which the error occurred
• Error_line - Line number in which the error occurred
• Error_context - Array containing every variable and their values in use when the
error occurred.
Page 6 out of 10Error and Exception Handling in PHP
Possible Error levels
• .E_ERROR - Fatal run-time errors
• E_WARNING - Non-fatal run-time errors
• E_PARSE - Compile-time parse errors
• E_NOTICE - Run-time notices.
• E_CORE_ERROR - Fatal errors that occur during PHP's initial start-up.
• E_CORE_WARNING - Non-fatal run-time errors. This occurs during PHP's initial
start-up.
• E_USER_ERROR - Fatal user-generated error.
• E_USER_WARNING - Non-fatal user-generated warning.
• E_USER_NOTICE - User-generated notice.
• E_ALL - All errors and warnings, except level E_STRICT
Page 7 out of 10Error and Exception Handling in PHP
Exceptions Handling
PHP 5 has an exception model similar to that of other programming languages.
Exceptions are important and provides a better control over error handling.
• Try − A function using an exception should be in a "try" block. If the exception does not
trigger, the code will continue as normal. However if the exception triggers, an exception is
"thrown".
• Throw − This is how you trigger an exception. Each "throw" must have at least one "catch".
• Catch − A "catch" block retrieves an exception and creates an object containing the exception
information.
Page 8 out of 10Error and Exception Handling in PHP
Creating Custom Exception Handler
We can define our own custom exception handler. By using following function to
set a user-defined exception handler function.
Syntax:
string set_exception_handler ( callback $exception_handler )
Example:
<?php
function exception_handler($exception) {
echo "Uncaught exception: " , $exception->getMessage(), "n";
}
set_exception_handler('exception_handler');
throw new Exception('Uncaught Exception');
echo "Not Executedn";
?>
Page 9 out of 10Error and Exception Handling in PHP
Page 10 out of 10Error and Exception Handling in PHP

More Related Content

What's hot

TRACY: AN ADDICTIVE TOOL TO EASE DEBUGGING
TRACY: AN ADDICTIVE TOOL TO EASE DEBUGGINGTRACY: AN ADDICTIVE TOOL TO EASE DEBUGGING
TRACY: AN ADDICTIVE TOOL TO EASE DEBUGGINGDavid Grudl
 
Error reporting in php
Error reporting in php Error reporting in php
Error reporting in php Mudasir Syed
 
Elegant Ways of Handling PHP Errors and Exceptions
Elegant Ways of Handling PHP Errors and ExceptionsElegant Ways of Handling PHP Errors and Exceptions
Elegant Ways of Handling PHP Errors and ExceptionsZendCon
 
Python Exception Handling
Python Exception HandlingPython Exception Handling
Python Exception HandlingMegha V
 
Exception Handling in Python - Rik van Achterberg & Tim Muller
Exception Handling in Python - Rik van Achterberg & Tim MullerException Handling in Python - Rik van Achterberg & Tim Muller
Exception Handling in Python - Rik van Achterberg & Tim MullerByte
 
Fundamentals of programming finals.ajang
Fundamentals of programming finals.ajangFundamentals of programming finals.ajang
Fundamentals of programming finals.ajangJaricka Angelyd Marquez
 
Exception handling in Python
Exception handling in PythonException handling in Python
Exception handling in PythonAdnan Siddiqi
 
Exception Handling In Python | Exceptions In Python | Python Programming Tuto...
Exception Handling In Python | Exceptions In Python | Python Programming Tuto...Exception Handling In Python | Exceptions In Python | Python Programming Tuto...
Exception Handling In Python | Exceptions In Python | Python Programming Tuto...Edureka!
 
Python Programming - X. Exception Handling and Assertions
Python Programming - X. Exception Handling and AssertionsPython Programming - X. Exception Handling and Assertions
Python Programming - X. Exception Handling and AssertionsRanel Padon
 
Presentation on-exception-handling
Presentation on-exception-handlingPresentation on-exception-handling
Presentation on-exception-handlingNahian Ahmed
 
1336333055 php tutorial_from_beginner_to_master
1336333055 php tutorial_from_beginner_to_master1336333055 php tutorial_from_beginner_to_master
1336333055 php tutorial_from_beginner_to_masterjeeva indra
 
Exception Handling
Exception HandlingException Handling
Exception Handlingbackdoor
 

What's hot (20)

PHP - Introduction to PHP Error Handling
PHP -  Introduction to PHP Error HandlingPHP -  Introduction to PHP Error Handling
PHP - Introduction to PHP Error Handling
 
TRACY: AN ADDICTIVE TOOL TO EASE DEBUGGING
TRACY: AN ADDICTIVE TOOL TO EASE DEBUGGINGTRACY: AN ADDICTIVE TOOL TO EASE DEBUGGING
TRACY: AN ADDICTIVE TOOL TO EASE DEBUGGING
 
Error reporting in php
Error reporting in php Error reporting in php
Error reporting in php
 
Elegant Ways of Handling PHP Errors and Exceptions
Elegant Ways of Handling PHP Errors and ExceptionsElegant Ways of Handling PHP Errors and Exceptions
Elegant Ways of Handling PHP Errors and Exceptions
 
Python Exception Handling
Python Exception HandlingPython Exception Handling
Python Exception Handling
 
Python exception handling
Python   exception handlingPython   exception handling
Python exception handling
 
Exception Handling in Python - Rik van Achterberg & Tim Muller
Exception Handling in Python - Rik van Achterberg & Tim MullerException Handling in Python - Rik van Achterberg & Tim Muller
Exception Handling in Python - Rik van Achterberg & Tim Muller
 
Fundamentals of programming finals.ajang
Fundamentals of programming finals.ajangFundamentals of programming finals.ajang
Fundamentals of programming finals.ajang
 
Exception handling in Python
Exception handling in PythonException handling in Python
Exception handling in Python
 
Exception Handling In Python | Exceptions In Python | Python Programming Tuto...
Exception Handling In Python | Exceptions In Python | Python Programming Tuto...Exception Handling In Python | Exceptions In Python | Python Programming Tuto...
Exception Handling In Python | Exceptions In Python | Python Programming Tuto...
 
Python Programming - X. Exception Handling and Assertions
Python Programming - X. Exception Handling and AssertionsPython Programming - X. Exception Handling and Assertions
Python Programming - X. Exception Handling and Assertions
 
Exceptions in python
Exceptions in pythonExceptions in python
Exceptions in python
 
Php tutorial
Php tutorialPhp tutorial
Php tutorial
 
Exception
ExceptionException
Exception
 
Presentation on-exception-handling
Presentation on-exception-handlingPresentation on-exception-handling
Presentation on-exception-handling
 
1336333055 php tutorial_from_beginner_to_master
1336333055 php tutorial_from_beginner_to_master1336333055 php tutorial_from_beginner_to_master
1336333055 php tutorial_from_beginner_to_master
 
Exception Handling
Exception HandlingException Handling
Exception Handling
 
Bioinformatica 27-10-2011-p4-files
Bioinformatica 27-10-2011-p4-filesBioinformatica 27-10-2011-p4-files
Bioinformatica 27-10-2011-p4-files
 
43c
43c43c
43c
 
Debugging With Php
Debugging With PhpDebugging With Php
Debugging With Php
 

Similar to Error and Exception Handling in PHP

Exception handling
Exception handlingException handling
Exception handlingMinal Maniar
 
Module-4_WTA_PHP Class & Error Handling
Module-4_WTA_PHP Class & Error HandlingModule-4_WTA_PHP Class & Error Handling
Module-4_WTA_PHP Class & Error HandlingSIVAKUMAR V
 
Error handling and debugging
Error handling and debuggingError handling and debugging
Error handling and debuggingsalissal
 
lecture2-PerlProgramming
lecture2-PerlProgramminglecture2-PerlProgramming
lecture2-PerlProgrammingtutorialsruby
 
lecture2-PerlProgramming
lecture2-PerlProgramminglecture2-PerlProgramming
lecture2-PerlProgrammingtutorialsruby
 
Exception Handling.pptx
Exception Handling.pptxException Handling.pptx
Exception Handling.pptxPavan326406
 
Winter%200405%20-%20Beginning%20PHP
Winter%200405%20-%20Beginning%20PHPWinter%200405%20-%20Beginning%20PHP
Winter%200405%20-%20Beginning%20PHPtutorialsruby
 
Winter%200405%20-%20Beginning%20PHP
Winter%200405%20-%20Beginning%20PHPWinter%200405%20-%20Beginning%20PHP
Winter%200405%20-%20Beginning%20PHPtutorialsruby
 
Php tutorial(w3schools)
Php tutorial(w3schools)Php tutorial(w3schools)
Php tutorial(w3schools)Arjun Shanka
 
Php tutorial from_beginner_to_master
Php tutorial from_beginner_to_masterPhp tutorial from_beginner_to_master
Php tutorial from_beginner_to_masterPrinceGuru MS
 
Introduction to PHP.pptx
Introduction to PHP.pptxIntroduction to PHP.pptx
Introduction to PHP.pptxSherinRappai
 
Session10-PHP Misconfiguration
Session10-PHP MisconfigurationSession10-PHP Misconfiguration
Session10-PHP Misconfigurationzakieh alizadeh
 

Similar to Error and Exception Handling in PHP (20)

Exception handling
Exception handlingException handling
Exception handling
 
Module-4_WTA_PHP Class & Error Handling
Module-4_WTA_PHP Class & Error HandlingModule-4_WTA_PHP Class & Error Handling
Module-4_WTA_PHP Class & Error Handling
 
Error handling and debugging
Error handling and debuggingError handling and debugging
Error handling and debugging
 
Basics PHP
Basics PHPBasics PHP
Basics PHP
 
lecture2-PerlProgramming
lecture2-PerlProgramminglecture2-PerlProgramming
lecture2-PerlProgramming
 
lecture2-PerlProgramming
lecture2-PerlProgramminglecture2-PerlProgramming
lecture2-PerlProgramming
 
Exception Handling.pptx
Exception Handling.pptxException Handling.pptx
Exception Handling.pptx
 
Winter%200405%20-%20Beginning%20PHP
Winter%200405%20-%20Beginning%20PHPWinter%200405%20-%20Beginning%20PHP
Winter%200405%20-%20Beginning%20PHP
 
Winter%200405%20-%20Beginning%20PHP
Winter%200405%20-%20Beginning%20PHPWinter%200405%20-%20Beginning%20PHP
Winter%200405%20-%20Beginning%20PHP
 
Error handling
Error handlingError handling
Error handling
 
Php tutorialw3schools
Php tutorialw3schoolsPhp tutorialw3schools
Php tutorialw3schools
 
Php tutorial(w3schools)
Php tutorial(w3schools)Php tutorial(w3schools)
Php tutorial(w3schools)
 
Php tutorial from_beginner_to_master
Php tutorial from_beginner_to_masterPhp tutorial from_beginner_to_master
Php tutorial from_beginner_to_master
 
Introduction to PHP.pptx
Introduction to PHP.pptxIntroduction to PHP.pptx
Introduction to PHP.pptx
 
Php manish
Php manishPhp manish
Php manish
 
Session10-PHP Misconfiguration
Session10-PHP MisconfigurationSession10-PHP Misconfiguration
Session10-PHP Misconfiguration
 
Exception handling
Exception handlingException handling
Exception handling
 
Php
PhpPhp
Php
 
Php
PhpPhp
Php
 
Php
PhpPhp
Php
 

More from Arafat Hossan

Data Dictionary in System Analysis and Design
Data Dictionary in System Analysis and DesignData Dictionary in System Analysis and Design
Data Dictionary in System Analysis and DesignArafat Hossan
 
Digital Data to Digital Signal Conversion
Digital Data to Digital Signal ConversionDigital Data to Digital Signal Conversion
Digital Data to Digital Signal ConversionArafat Hossan
 
Bus Interface Unit(BIU) of 8086 Microprocessor
Bus Interface Unit(BIU) of 8086 MicroprocessorBus Interface Unit(BIU) of 8086 Microprocessor
Bus Interface Unit(BIU) of 8086 MicroprocessorArafat Hossan
 
Mapping cardinalities
Mapping cardinalitiesMapping cardinalities
Mapping cardinalitiesArafat Hossan
 
Relational algebra in DBMS
Relational algebra in DBMSRelational algebra in DBMS
Relational algebra in DBMSArafat Hossan
 
Processes in Operating System
Processes in Operating SystemProcesses in Operating System
Processes in Operating SystemArafat Hossan
 
All pair shortest path
All pair shortest pathAll pair shortest path
All pair shortest pathArafat Hossan
 
Job sequencing with deadline
Job sequencing with deadlineJob sequencing with deadline
Job sequencing with deadlineArafat Hossan
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in javaArafat Hossan
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in javaArafat Hossan
 

More from Arafat Hossan (20)

Data Dictionary in System Analysis and Design
Data Dictionary in System Analysis and DesignData Dictionary in System Analysis and Design
Data Dictionary in System Analysis and Design
 
Digital Data to Digital Signal Conversion
Digital Data to Digital Signal ConversionDigital Data to Digital Signal Conversion
Digital Data to Digital Signal Conversion
 
Bus Interface Unit(BIU) of 8086 Microprocessor
Bus Interface Unit(BIU) of 8086 MicroprocessorBus Interface Unit(BIU) of 8086 Microprocessor
Bus Interface Unit(BIU) of 8086 Microprocessor
 
Assembly language
Assembly languageAssembly language
Assembly language
 
Web frameworks
Web frameworksWeb frameworks
Web frameworks
 
Cpu scheduling
Cpu schedulingCpu scheduling
Cpu scheduling
 
Semaphore
SemaphoreSemaphore
Semaphore
 
Deadlock
DeadlockDeadlock
Deadlock
 
Data model
Data modelData model
Data model
 
Mapping cardinalities
Mapping cardinalitiesMapping cardinalities
Mapping cardinalities
 
Sql in dbms
Sql in dbmsSql in dbms
Sql in dbms
 
Relational algebra in DBMS
Relational algebra in DBMSRelational algebra in DBMS
Relational algebra in DBMS
 
Divisible rules
Divisible rulesDivisible rules
Divisible rules
 
Processes in Operating System
Processes in Operating SystemProcesses in Operating System
Processes in Operating System
 
All pair shortest path
All pair shortest pathAll pair shortest path
All pair shortest path
 
Asymptotic notation
Asymptotic notationAsymptotic notation
Asymptotic notation
 
Merge sort
Merge sortMerge sort
Merge sort
 
Job sequencing with deadline
Job sequencing with deadlineJob sequencing with deadline
Job sequencing with deadline
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
 
Multithreading in java
Multithreading in javaMultithreading in java
Multithreading in java
 

Recently uploaded

Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxJoão Esperancinha
 
Call Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call GirlsCall Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call Girlsssuser7cb4ff
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfAsst.prof M.Gokilavani
 
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...ZTE
 
Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.eptoze12
 
chaitra-1.pptx fake news detection using machine learning
chaitra-1.pptx  fake news detection using machine learningchaitra-1.pptx  fake news detection using machine learning
chaitra-1.pptx fake news detection using machine learningmisbanausheenparvam
 
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130Suhani Kapoor
 
Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...VICTOR MAESTRE RAMIREZ
 
microprocessor 8085 and its interfacing
microprocessor 8085  and its interfacingmicroprocessor 8085  and its interfacing
microprocessor 8085 and its interfacingjaychoudhary37
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...srsj9000
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024Mark Billinghurst
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidNikhilNagaraju
 
Artificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxArtificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxbritheesh05
 
Current Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLCurrent Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLDeelipZope
 
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝soniya singh
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )Tsuyoshi Horigome
 

Recently uploaded (20)

Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
 
Call Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call GirlsCall Girls Narol 7397865700 Independent Call Girls
Call Girls Narol 7397865700 Independent Call Girls
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
 
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
 
Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.Oxy acetylene welding presentation note.
Oxy acetylene welding presentation note.
 
chaitra-1.pptx fake news detection using machine learning
chaitra-1.pptx  fake news detection using machine learningchaitra-1.pptx  fake news detection using machine learning
chaitra-1.pptx fake news detection using machine learning
 
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
 
Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...
 
microprocessor 8085 and its interfacing
microprocessor 8085  and its interfacingmicroprocessor 8085  and its interfacing
microprocessor 8085 and its interfacing
 
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Serviceyoung call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
 
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfid
 
Artificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxArtificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptx
 
Current Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLCurrent Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCL
 
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
 
SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )SPICE PARK APR2024 ( 6,793 SPICE Models )
SPICE PARK APR2024 ( 6,793 SPICE Models )
 

Error and Exception Handling in PHP

  • 1. Topic: Error and Exception Handling in PHP Error and Exception Handling in PHP
  • 2. Contents • Error and Error Handling • Using die() function • Defining Custom Error Handling Function • Error Parameter • Possible Error levels • Possible Error levels Exceptions Handling • Creating Custom Exception Handler Page 2 out of 10Error and Exception Handling in PHP
  • 3. Error and Error Handling • An error is something you have done which is considered to be incorrect or wrong, or which should not have been done. • Error handling is the process of catching errors raised by our program and then taking appropriate action to prevent unforeseen consequences. Page 3 out of 10Error and Exception Handling in PHP
  • 4. Using die() function By using die() function we can stop the process or jump to the next process if there is a error. Normally it is used when file is not found. Example: Page 4 out of 10 if(!file_exists("/tmp/test.txt")) { die("File not found"); } else { $file = fopen("/tmp/test.txt","r"); print "Opend file; } Error and Exception Handling in PHP
  • 5. Defining Custom Error Handling Function • We can write our own function to handling any error. PHP provides us a framework to define error handling function. • This function must be able to handle minimum two parameters (error level and error message) but can accept up to five parameters (optionally: file, line- number, and the error context) Syntax: Error_Function_Name(Error_Level, Error_Message, Error_File, Error_Line, Error_Context) Page 5 out of 10Error and Exception Handling in PHP
  • 6. Error Parameter There are 5 kinds of error parameters • Error_level - A value number, specifies the error report level for the user-defined error. • Error_message - Message for the user-defined error • Error_file - File name in which the error occurred • Error_line - Line number in which the error occurred • Error_context - Array containing every variable and their values in use when the error occurred. Page 6 out of 10Error and Exception Handling in PHP
  • 7. Possible Error levels • .E_ERROR - Fatal run-time errors • E_WARNING - Non-fatal run-time errors • E_PARSE - Compile-time parse errors • E_NOTICE - Run-time notices. • E_CORE_ERROR - Fatal errors that occur during PHP's initial start-up. • E_CORE_WARNING - Non-fatal run-time errors. This occurs during PHP's initial start-up. • E_USER_ERROR - Fatal user-generated error. • E_USER_WARNING - Non-fatal user-generated warning. • E_USER_NOTICE - User-generated notice. • E_ALL - All errors and warnings, except level E_STRICT Page 7 out of 10Error and Exception Handling in PHP
  • 8. Exceptions Handling PHP 5 has an exception model similar to that of other programming languages. Exceptions are important and provides a better control over error handling. • Try − A function using an exception should be in a "try" block. If the exception does not trigger, the code will continue as normal. However if the exception triggers, an exception is "thrown". • Throw − This is how you trigger an exception. Each "throw" must have at least one "catch". • Catch − A "catch" block retrieves an exception and creates an object containing the exception information. Page 8 out of 10Error and Exception Handling in PHP
  • 9. Creating Custom Exception Handler We can define our own custom exception handler. By using following function to set a user-defined exception handler function. Syntax: string set_exception_handler ( callback $exception_handler ) Example: <?php function exception_handler($exception) { echo "Uncaught exception: " , $exception->getMessage(), "n"; } set_exception_handler('exception_handler'); throw new Exception('Uncaught Exception'); echo "Not Executedn"; ?> Page 9 out of 10Error and Exception Handling in PHP
  • 10. Page 10 out of 10Error and Exception Handling in PHP