web based programming - php error and exception handling
TRANSCRIPT
TRINITY INSTITUTE OF PROFESSIONAL STUDIES
Sector – 9, Dwarka Institutional Area, New Delhi-75 Affiliated G.G.S.IP.U, Delhi
By: Shweta Bhardwaj
WEB BASED PROGRAMMING
PAPER ID: 20313
TRINITY INSTITUTE OF PROFESSIONAL STUDIESSector – 9, Dwarka Institutional Area, New Delhi-75
PHP Error and
Exception Handling
TRINITY INSTITUTE OF PROFESSIONAL STUDIESSector – 9, Dwarka Institutional Area, New Delhi-75
• Error handling is the process of catching errors raised by your program and then taking appropriate action. If you would handle errors properly then it may lead to many unforeseen consequences.
• Its very simple in PHP to handle an errors.
TRINITY INSTITUTE OF PROFESSIONAL STUDIESSector – 9, Dwarka Institutional Area, New Delhi-75
Returning errors from functions
• Php in built functions return false when error occurs.
• When a function returns false we can print error message using DIE() function.
The die() function prints a message and exits the current script.
TRINITY INSTITUTE OF PROFESSIONAL STUDIESSector – 9, Dwarka Institutional Area, New Delhi-75
SYNTAX
• die(message)
Message Required. Specifies the message or status
number to write before exiting the script. The status number will not be written to the output.
TRINITY INSTITUTE OF PROFESSIONAL STUDIESSector – 9, Dwarka Institutional Area, New Delhi-75
Example
• <?php$site = "http://www.w3schools.com/";
fopen($site,"r")or die("Unable to connect to $site");?>
TRINITY INSTITUTE OF PROFESSIONAL STUDIESSector – 9, Dwarka Institutional Area, New Delhi-75
<?php if(!file_exists("/tmp/test.txt")) { die("File not found"); } else { $file=fopen("/tmp/test.txt","r"); print "Opend file sucessfully"; } // Test of the code here. ?>
TRINITY INSTITUTE OF PROFESSIONAL STUDIESSector – 9, Dwarka Institutional Area, New Delhi-75