web based programming - php error and exception handling

8
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

Upload: trinity-dwarka

Post on 09-Feb-2017

119 views

Category:

Education


1 download

TRANSCRIPT

Page 1: WEB BASED PROGRAMMING - PHP Error and Exception Handling

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

Page 2: WEB BASED PROGRAMMING - PHP Error and Exception Handling

TRINITY INSTITUTE OF PROFESSIONAL STUDIESSector – 9, Dwarka Institutional Area, New Delhi-75

PHP Error and

Exception Handling

Page 3: WEB BASED PROGRAMMING - 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.

Page 4: WEB BASED PROGRAMMING - PHP Error and Exception Handling

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.

Page 5: WEB BASED PROGRAMMING - PHP Error and Exception Handling

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.

Page 6: WEB BASED PROGRAMMING - PHP Error and Exception Handling

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");?>

Page 7: WEB BASED PROGRAMMING - PHP Error and Exception Handling

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. ?>

Page 8: WEB BASED PROGRAMMING - PHP Error and Exception Handling

TRINITY INSTITUTE OF PROFESSIONAL STUDIESSector – 9, Dwarka Institutional Area, New Delhi-75