TRINITY INSTITUTE OF PROFESSIONAL
STUDIESSector – 9, DwarkaInstitutional Area, New Delhi-75
Affiliated G.G.S.IP.U, Delhi
By: ShwetaBhardwaj
WEB BASED
PROGRAMMING
PAPER ID: 20313
TRINITY INSTITUTE OF PROFESSIONAL STUDIES
Sector – 9, DwarkaInstitutional Area, New Delhi-75
PHP Error
and
Exception Handling
TRINITY INSTITUTE OF PROFESSIONAL STUDIES
Sector – 9, DwarkaInstitutional 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 STUDIES
Sector – 9, DwarkaInstitutional 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 STUDIES
Sector – 9, DwarkaInstitutional 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 STUDIES
Sector – 9, DwarkaInstitutional 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 STUDIES
Sector – 9, DwarkaInstitutional 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 STUDIES
Sector – 9, DwarkaInstitutional Area, New Delhi-75

WEB BASED PROGRAMMING - PHP Error and Exception Handling

  • 1.
    TRINITY INSTITUTE OFPROFESSIONAL STUDIESSector – 9, DwarkaInstitutional Area, New Delhi-75 Affiliated G.G.S.IP.U, Delhi By: ShwetaBhardwaj WEB BASED PROGRAMMING PAPER ID: 20313
  • 2.
    TRINITY INSTITUTE OFPROFESSIONAL STUDIES Sector – 9, DwarkaInstitutional Area, New Delhi-75 PHP Error and Exception Handling
  • 3.
    TRINITY INSTITUTE OFPROFESSIONAL STUDIES Sector – 9, DwarkaInstitutional 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.
  • 4.
    TRINITY INSTITUTE OFPROFESSIONAL STUDIES Sector – 9, DwarkaInstitutional 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.
  • 5.
    TRINITY INSTITUTE OFPROFESSIONAL STUDIES Sector – 9, DwarkaInstitutional 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.
  • 6.
    TRINITY INSTITUTE OFPROFESSIONAL STUDIES Sector – 9, DwarkaInstitutional Area, New Delhi-75 Example • <?php $site = "http://www.w3schools.com/"; fopen($site,"r") or die("Unable to connect to $site"); ?>
  • 7.
    TRINITY INSTITUTE OFPROFESSIONAL STUDIES Sector – 9, DwarkaInstitutional 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. ?>
  • 8.
    TRINITY INSTITUTE OFPROFESSIONAL STUDIES Sector – 9, DwarkaInstitutional Area, New Delhi-75