Explanation of exception handling in PHP5 (1) _ PHP Tutorial

Source: Internet
Author: User
Exception handling in PHP5 (1 ). First, try, catch <? Php $ pathD: \ in.txt; try to detect exceptions {file_open ($ path);} catch (Exception $ e) catch exceptions {echo $ e-> getMessage ();} functionfile_open ($ path) first is try, catch

<? Php
$ Path = "D: \ in.txt ";
Try // detect exceptions
{
File_open ($ path );
}
Catch (Exception $ e) // catch an Exception
{
Echo $ e-> getMessage ();
}
Function file_open ($ path)
{
If (! File_exists ($ path) // if the file cannot be found, an exception object is thrown.
{
Throw new Exception ("the file cannot be found", 1 );
}
If (! Fopen ($ path, "r") // if the file cannot be opened, an exception is thrown.
{
Throw new Exception ("file cannot be opened", 2 );
}
}
? >

Use $ e-> getMessage () to output exception information.

Output complete exception information

﹤?php 
$path = "D:\\in.txt";
Try
{
File_open ($ path); // try to open the file
}
Catch (Exception $ e)
{
Echo "exception information:". $ e-> getMessage (). "\ n"; // return user-defined exception information
Echo "exception code:". $ e-> getCode (). "\ n"; // return the custom exception code
Echo "File name:". $ e-> getFile (). "\ n"; // return the PHP program file name with an exception
Echo "row where the exception code is located". $ e-> getLine (). "\ n"; // return the row number of the row where the exception code is located.
Echo "route :";
Print_r ($ e-> getTrace (); // returns the route passed by each step of the trace exception in the form of an array
Echo $ e-> getTraceAsString (); // returns the getTrace function information formatted as a string.
}
Function file_open ($ path)
{
If (! File_exists ($ path) // if the file does not exist, an error is thrown.
{
Throw new Exception ("the file cannot be found", 1 );
}
If (! Fopen ($ path, "r "))
{
Throw new Exception ("file cannot be opened", 2 );
}
}
? >
1

? Php $ path = "D :\\ in.txt"; try // detect exceptions {file_open ($ path);} catch (Exception $ e) // capture exceptions {echo $ e-> getMessage ();} function file_open ($ path )...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.