Php exception handling method example _ PHP Tutorial

Source: Internet
Author: User
Tags php exception handling
Summary of php exception handling method instances. Php exception handling method example summary this article describes the php exception handling method. Share it with you for your reference. The details are as follows :? 1234567891011121314151617181920 php exception handling method instance summary

This article describes how to handle php exceptions. Share it with you for your reference. The details are as follows:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

$ 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 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 );

}

}

?>

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

$ Path = "D: // in.txt ";

// File path

File_open ($ path );

// Call the file_open function

Function file_open ($ path)

{

If (! File_exists ($ path ))

// If the file cannot be found, an exception 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 );

}

}

?>

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

Function exception_handler ($ e)

// Functions used to handle exceptions

{

Echo "uncaptured exceptions:". $ e-> getMessage ();

}

Set_exception_handler ("exception_handler ");

// Set The Exception handling function

Try // detect exceptions

{

$ Path = "D: // in.txt ";

}

Catch (Exception $ e) // catch an Exception

{

Echo $ e-> getMessage ();

}

File_open ($ path); // call a function to open a file

Function file_open ($ path)

{

If (! File_exists ($ path ))

// If the file cannot be found, an exception 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 );

}

}

?>

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

$ 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 user-defined exception code

Echo "File name:". $ e-> getFile (). "/n ";

// Return the PHP program file name with an exception

Echo "abnormal code row". $ e-> getLine (). "/n ";

// Return the row number of the row where the code with an exception is located.

Echo "route :";

Print_r ($ e-> getTrace ());

// Return 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 object 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

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

Class FileExistsException extends Exception {}

// Class used to handle non-abnormal file

Class FileOpenException extends Exception {}

// Class used to handle unreadable file exceptions

$ Path = "D: // in.txt ";

Try

{

File_open ($ path );

}

Catch (FileExistsException $ e)

// If a FileExistsException exception occurs, the system prompts the user to confirm the file location.

{

Echo "An exception occurred when the program was running:". $ e-> getMessage (). "/n ";

Echo "please confirm the file location. ";

}

Catch (FileOpenException $ e)

// If a FileOpenException occurs, the system prompts the user to check the readability of the file.

{

Echo "An exception occurred when the program was running:". $ e-> getMessage (). "/n ";

Echo "make sure the file is readable. ";

}

Catch (Exception $ e)

{

Echo "[unknown exception]";

Echo "exception information:". $ e-> getMessage (). "/n ";

// Return user-defined exception information

Echo "exception code:". $ e-> getCode (). "/n ";

// Return the user-defined exception code

Echo "File name:". $ e-> getFile (). "/n ";

// Return the PHP program file name with an exception

Echo "abnormal code row". $ e-> getLine (). "/n ";

// Return the row number of the row where the code with an exception is located.

Echo "route :";

Print_r ($ e-> getTrace ());

// Return 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 ))

{

Throw new FileExistsException ("file cannot be found", 1 );

// Throw a FileExistsException exception object

}

If (! Fopen ($ path, "r "))

{

Throw new FileOpenException ("file cannot be opened", 2 );

// Throw a FileOpenException object

}

}

?>

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

Class FileExistsException extends Exception {}

// Class used to handle non-abnormal file

Class FileOpenException extends Exception {}

// Class used to handle unreadable file exceptions

$ Path = "D: // in.txt ";

Try

{

File_open ($ path); // try to open the file

}

Catch (Exception $ e)

{

Echo "[unknown exception]";

Echo "exception information:". $ e-> getMessage (). "/n ";

// Return user-defined exception information

Echo "exception code:". $ e-> getCode (). "/n ";

// Return the user-defined exception code

Echo "File name:". $ e-> getFile (). "/n ";

// Return the PHP program file name with an exception

Echo "abnormal code row". $ e-> getLine (). "/n ";

// Return the row number of the row where the code with an exception is located.

Echo "route :";

Print_r ($ e-> getTrace ());

// Return 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.

}

Catch (FileExistsException $ e)

// If a FileExistsException exception occurs, the system prompts the user to confirm the file location.

{

Echo "An exception occurred when the program was running:". $ e-> getMessage (). "/n ";

Echo "please confirm the file location. ";

}

Catch (FileOpenException $ e)

// If a FileOpenException occurs, the system prompts the user to check the readability of the file.

{

Echo "An exception occurred when the program was running:". $ e-> getMessage (). "/n ";

Echo "make sure the file is readable. ";

}

Function file_open ($ path)

{

If (! File_exists ($ path ))

// If the file does not exist, the output is incorrect.

{

Throw new FileExistsException ("file cannot be found", 1 );

}

If (! Fopen ($ path, "r "))

{

Throw new FileOpenException ("file cannot be opened", 2 );

}

}

?>

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

Class FileExistsException extends Exception {}

// Class used to handle non-abnormal file

Class FileOpenException extends Exception {}

// Class used to handle unreadable file exceptions

$ Path = "D: // in.txt ";

Try

{

File_open ($ path );

}

Catch (FileExistsException $ e)

// If a FileExistsException exception occurs, the system prompts the user to confirm the file location.

{

Echo "An exception occurred when the program was running:". $ e-> getMessage (). "/n ";

Echo "please confirm the file location. ";

}

Catch (FileOpenException $ e)

// If a FileOpenException occurs, the system prompts the user to check the readability of the file.

{

Echo "An exception occurred when the program was running:". $ e-> getMessage (). "/n ";

Echo "make sure the file is readable. ";

}

Catch (Exception $ e)

{

Echo "[unknown exception]";

Echo "exception information:". $ e-> getMessage (). "/n ";

// Return user-defined exception information

Echo "exception code:". $ e-> getCode (). "/n ";

// Return the user-defined exception code

Echo "File name:". $ e-> getFile (). "/n ";

// Return the PHP program file name with an exception

Echo "abnormal code row". $ e-> getLine (). "/n ";

// Return the row number of the row where the code with an exception is located.

Echo "route :";

Print_r ($ e-> getTrace ());

// Return 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)

{

Try

{

If (! File_exists ($ path ))

{

Throw new FileExistsException ("file cannot be found", 1 );

}

If (! Fopen ($ path, "r "))

{

Throw new FileOpenException ("file cannot be opened", 2 );

}

}

Catch (Exception $ e) // catch an Exception

{

Echo "the file_open function encountered an exception while running ";

Throw $ e; // throwing an exception

}

}

?>

I hope this article will help you with php programming.

Examples in this article describes how to handle php exceptions. Share it with you for your reference. The details are as follows :? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 19 20...

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.