search
HomeBackend DevelopmentPHP TutorialSummary of examples of php exception handling methods_PHP tutorial

A summary of examples of PHP exception handling methods

This article describes PHP exception handling methods through examples. Share it with everyone 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 //检测异常

{

file_open($path);

}

catch(Exception $e) //捕获异常

{

echo $e->getMessage();

}

 

function file_open($path)

{

if(!file_exists($path))

//如果文件无法找到,抛出异常对象

{

throw new Exception("文件无法找到", 1);

}

if(!fopen($path, "r"))

//如果文件无法打开,抛出异常对象

{

throw new Exception("文件无法打开", 2);

}

}

?>

1 2

3

4

5

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_open($path);

//调用file_open函数

 

function file_open($path)

{

if(!file_exists($path))

//如果文件无法找到,抛出异常对象

{

throw new Exception("文件无法找到", 1);

}

if(!fopen($path, "r"))

//如果文件无法打开,抛出异常对象

{

throw new Exception("文件无法打开", 2);

}

}

?>

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 exception { file_open($path); } catch(Exception $e) //Catch exception { echo $e->getMessage(); } function file_open($path) { if(!file_exists($path)) //If the file cannot be found, throw an exception object { throw new Exception("File cannot be found", 1); } if(!fopen($path, "r")) //If the file cannot be opened, throw an exception object { 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"; //The path where the file is located file_open($path); //Call the file_open function function file_open($path) { if(!file_exists($path)) //If the file cannot be found, throw an exception object { throw new Exception("File cannot be found", 1); } if(!fopen($path, "r")) //If the file cannot be opened, throw an exception object { 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)

//用于处理异常的函数

{

echo "未捕获的异常:".$e->getMessage();

}

 

set_exception_handler("exception_handler");

//设置异常处理函数

try //检测异常

{

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

}

catch(Exception $e) //捕获异常

{

echo $e->getMessage();

}

file_open($path); //调用函数打开文件

function file_open($path)

{

if(!file_exists($path))

//如果文件无法找到,抛出异常对象

{

throw new Exception("文件无法找到", 1);

}

if(!fopen($path, "r"))

//如果文件无法打开,抛出异常对象

{

throw new Exception("文件无法打开", 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) //用于处理异常的函数 { echo "未捕获的异常:".$e->getMessage(); }   set_exception_handler("exception_handler"); //设置异常处理函数 try //检测异常 { $path = "D://in.txt"; } catch(Exception $e) //捕获异常 { echo $e->getMessage(); } file_open($path); //调用函数打开文件 function file_open($path) { if(!file_exists($path)) //如果文件无法找到,抛出异常对象 { throw new Exception("文件无法找到", 1); } if(!fopen($path, "r")) //如果文件无法打开,抛出异常对象 { throw new Exception("文件无法打开", 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); //尝试打开文件

}

catch(Exception $e)

{

echo "异常信息:".$e->getMessage()."/n";

//返回用户自定义的异常信息

echo "异常代码:".$e->getCode()."/n";

//返回用户自定义的异常代码

echo "文件名:".$e->getFile()."/n";

//返回发生异常的PHP程序文件名

echo "异常代码所在行".$e->getLine()."/n";

//返回发生异常的代码所在行的行号

echo "传递路线:";

print_r($e->getTrace());

//以数组形式返回跟踪异常每一步传递的路线

echo $e->getTraceAsString();

//返回格式化成字符串的getTrace函数信息

}

function file_open($path)

{

if(!file_exists($path))

//如果文件不存在,则抛出错误

{

throw new Exception("文件无法找到", 1);

}

 

if(!fopen($path, "r"))

{

throw new Exception("文件无法打开", 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); //尝试打开文件 } catch(Exception $e) { echo "异常信息:".$e->getMessage()."/n"; //返回用户自定义的异常信息 echo "异常代码:".$e->getCode()."/n"; //返回用户自定义的异常代码 echo "文件名:".$e->getFile()."/n"; //返回发生异常的PHP程序文件名 echo "异常代码所在行".$e->getLine()."/n"; //返回发生异常的代码所在行的行号 echo "传递路线:"; print_r($e->getTrace()); //以数组形式返回跟踪异常每一步传递的路线 echo $e->getTraceAsString(); //返回格式化成字符串的getTrace函数信息 } function file_open($path) { if(!file_exists($path)) //如果文件不存在,则抛出错误 { throw new Exception("文件无法找到", 1); }   if(!fopen($path, "r")) { throw new Exception("文件无法打开", 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 FileOpenException extends Exception{}

//用于处理文件不可读异常的类

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

try

{

file_open($path);

}

catch(FileExistsException $e)

//如果产生FileExistsException异常则提示用户确认文件位置

{

echo "程序在运行过程中发生了异常:".$e->getMessage()."/n";

echo "请确认文件位置。";

}

catch(FileOpenException $e)

//如果产生FileOpenException异常则提示用户确认文件的可读性

{

echo "程序在运行过程中发生了异常:".$e->getMessage()."/n";

echo "请确认文件的可读性。";

}

catch(Exception $e)

{

echo "[未知异常]";

echo "异常信息:".$e->getMessage()."/n";

//返回用户自定义的异常信息

echo "异常代码:".$e->getCode()."/n";

//返回用户自定义的异常代码

echo "文件名:".$e->getFile()."/n";

//返回发生异常的PHP程序文件名

echo "异常代码所在行".$e->getLine()."/n";

//返回发生异常的代码所在行的行号

echo "传递路线:";

print_r($e->getTrace());

//以数组形式返回跟踪异常每一步传递的路线

echo $e->getTraceAsString();

//返回格式化成字符串的getTrace函数信息

}

function file_open($path)

{

if(!file_exists($path))

{

throw new FileExistsException("文件无法找到", 1);

//抛出FileExistsException异常对象

}

 

if(!fopen($path, "r"))

{

throw new FileOpenException("文件无法打开", 2);

//抛出FileOpenException异常对象

 

}

}

?>

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 FileOpenException extends Exception{} //用于处理文件不可读异常的类 $path = "D://in.txt"; try { file_open($path); } catch(FileExistsException $e) //如果产生FileExistsException异常则提示用户确认文件位置 { echo "程序在运行过程中发生了异常:".$e->getMessage()."/n"; echo "请确认文件位置。"; } catch(FileOpenException $e) //如果产生FileOpenException异常则提示用户确认文件的可读性 { echo "程序在运行过程中发生了异常:".$e->getMessage()."/n"; echo "请确认文件的可读性。"; } catch(Exception $e) { echo "[未知异常]"; echo "异常信息:".$e->getMessage()."/n"; //返回用户自定义的异常信息 echo "异常代码:".$e->getCode()."/n"; //返回用户自定义的异常代码 echo "文件名:".$e->getFile()."/n"; //返回发生异常的PHP程序文件名 echo "异常代码所在行".$e->getLine()."/n"; //返回发生异常的代码所在行的行号 echo "传递路线:"; print_r($e->getTrace()); //以数组形式返回跟踪异常每一步传递的路线 echo $e->getTraceAsString(); //返回格式化成字符串的getTrace函数信息 } function file_open($path) { if(!file_exists($path)) { throw new FileExistsException("文件无法找到", 1); //抛出FileExistsException异常对象 }   if(!fopen($path, "r")) { throw new FileOpenException("文件无法打开", 2); //抛出FileOpenException异常对象   } } ?>

?

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 FileOpenException extends Exception{}

//用于处理文件不可读异常的类

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

try

{

file_open($path); //尝试打开文件

}

catch(Exception $e)

{

echo "[未知异常]";

echo "异常信息:".$e->getMessage()."/n";

//返回用户自定义的异常信息

echo "异常代码:".$e->getCode()."/n";

//返回用户自定义的异常代码

echo "文件名:".$e->getFile()."/n";

//返回发生异常的PHP程序文件名

echo "异常代码所在行".$e->getLine()."/n";

//返回发生异常的代码所在行的行号

echo "传递路线:";

print_r($e->getTrace());

//以数组形式返回跟踪异常每一步传递的路线

echo $e->getTraceAsString();

//返回格式化成字符串的getTrace函数信息

}

catch(FileExistsException $e)

//如果产生FileExistsException异常则提示用户确认文件位置

{

echo "程序在运行过程中发生了异常:".$e->getMessage()."/n";

echo "请确认文件位置。";

}

catch(FileOpenException $e)

//如果产生FileOpenException异常则提示用户确认文件的可读性

{

echo "程序在运行过程中发生了异常:".$e->getMessage()."/n";

echo "请确认文件的可读性。";

}

function file_open($path)

{

if(!file_exists($path))

//如果文件不存在,则输出错误

{

throw new FileExistsException("文件无法找到", 1);

}

 

if(!fopen($path, "r"))

{

throw new FileOpenException("文件无法打开", 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
class FileExistsException extends Exception{} //用于处理文件不存在异常的类 class FileOpenException extends Exception{} //用于处理文件不可读异常的类 $path = "D://in.txt"; try { file_open($path); //尝试打开文件 } catch(Exception $e) { echo "[未知异常]"; echo "异常信息:".$e->getMessage()."/n"; //返回用户自定义的异常信息 echo "异常代码:".$e->getCode()."/n"; //返回用户自定义的异常代码 echo "文件名:".$e->getFile()."/n"; //返回发生异常的PHP程序文件名 echo "异常代码所在行".$e->getLine()."/n"; //返回发生异常的代码所在行的行号 echo "传递路线:"; print_r($e->getTrace()); //以数组形式返回跟踪异常每一步传递的路线 echo $e->getTraceAsString(); //返回格式化成字符串的getTrace函数信息 } catch(FileExistsException $e) //如果产生FileExistsException异常则提示用户确认文件位置 { echo "程序在运行过程中发生了异常:".$e->getMessage()."/n"; echo "请确认文件位置。"; } catch(FileOpenException $e) //如果产生FileOpenException异常则提示用户确认文件的可读性 { echo "程序在运行过程中发生了异常:".$e->getMessage()."/n"; echo "请确认文件的可读性。"; } function file_open($path) { if(!file_exists($path)) //如果文件不存在,则输出错误 { throw new FileExistsException("文件无法找到", 1); }   if(!fopen($path, "r")) { throw new FileOpenException("文件无法打开", 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 FileOpenException extends Exception{}

//用于处理文件不可读异常的类

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

try

{

file_open($path);

}

catch(FileExistsException $e)

//如果产生FileExistsException异常则提示用户确认文件位置

{

echo "程序在运行过程中发生了异常:".$e->getMessage()."/n";

echo "请确认文件位置。";

}

catch(FileOpenException $e)

//如果产生FileOpenException异常则提示用户确认文件的可读性

{

echo "程序在运行过程中发生了异常:".$e->getMessage()."/n";

echo "请确认文件的可读性。";

}

catch(Exception $e)

{

echo "[未知异常]";

echo "异常信息:".$e->getMessage()."/n";

//返回用户自定义的异常信息

echo "异常代码:".$e->getCode()."/n";

//返回用户自定义的异常代码

echo "文件名:".$e->getFile()."/n";

//返回发生异常的PHP程序文件名

echo "异常代码所在行".$e->getLine()."/n";

//返回发生异常的代码所在行的行号

echo "传递路线:";

print_r($e->getTrace());

//以数组形式返回跟踪异常每一步传递的路线

echo $e->getTraceAsString();

//返回格式化成字符串的getTrace函数信息

}

function file_open($path)

{

try

{

if(!file_exists($path))

{

throw new FileExistsException("文件无法找到", 1);

}

 

if(!fopen($path, "r"))

{

throw new FileOpenException("文件无法打开", 2);

}

}

catch(Exception $e) //捕获异常

{

echo "file_open函数在运行过程中出现异常";

throw $e; //重掷异常

}

}

?>

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 FileOpenException extends Exception{} //用于处理文件不可读异常的类 $path = "D://in.txt"; try { file_open($path); } catch(FileExistsException $e) //如果产生FileExistsException异常则提示用户确认文件位置 { echo "程序在运行过程中发生了异常:".$e->getMessage()."/n"; echo "请确认文件位置。"; } catch(FileOpenException $e) //如果产生FileOpenException异常则提示用户确认文件的可读性 { echo "程序在运行过程中发生了异常:".$e->getMessage()."/n"; echo "请确认文件的可读性。"; } catch(Exception $e) { echo "[未知异常]"; echo "异常信息:".$e->getMessage()."/n"; //返回用户自定义的异常信息 echo "异常代码:".$e->getCode()."/n"; //返回用户自定义的异常代码 echo "文件名:".$e->getFile()."/n"; //返回发生异常的PHP程序文件名 echo "异常代码所在行".$e->getLine()."/n"; //返回发生异常的代码所在行的行号 echo "传递路线:"; print_r($e->getTrace()); //以数组形式返回跟踪异常每一步传递的路线 echo $e->getTraceAsString(); //返回格式化成字符串的getTrace函数信息 } function file_open($path) { try { if(!file_exists($path)) { throw new FileExistsException("文件无法找到", 1); }   if(!fopen($path, "r")) { throw new FileOpenException("文件无法打开", 2); } } catch(Exception $e) //捕获异常 { echo "file_open函数在运行过程中出现异常"; throw $e; //重掷异常 } } ?>

I hope this article will be helpful to everyone’s PHP programming.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1020889.htmlTechArticleA summary of examples of PHP exception handling methods. This example describes the PHP exception handling method. Share it with everyone 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...
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
PHP Dependency Injection Container: A Quick StartPHP Dependency Injection Container: A Quick StartMay 13, 2025 am 12:11 AM

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Dependency Injection vs. Service Locator in PHPDependency Injection vs. Service Locator in PHPMay 13, 2025 am 12:10 AM

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHP performance optimization strategies.PHP performance optimization strategies.May 13, 2025 am 12:06 AM

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHP Email Validation: Ensuring Emails Are Sent CorrectlyPHP Email Validation: Ensuring Emails Are Sent CorrectlyMay 13, 2025 am 12:06 AM

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl

How to make PHP applications fasterHow to make PHP applications fasterMay 12, 2025 am 12:12 AM

TomakePHPapplicationsfaster,followthesesteps:1)UseOpcodeCachinglikeOPcachetostoreprecompiledscriptbytecode.2)MinimizeDatabaseQueriesbyusingquerycachingandefficientindexing.3)LeveragePHP7 Featuresforbettercodeefficiency.4)ImplementCachingStrategiessuc

PHP Performance Optimization Checklist: Improve Speed NowPHP Performance Optimization Checklist: Improve Speed NowMay 12, 2025 am 12:07 AM

ToimprovePHPapplicationspeed,followthesesteps:1)EnableopcodecachingwithAPCutoreducescriptexecutiontime.2)ImplementdatabasequerycachingusingPDOtominimizedatabasehits.3)UseHTTP/2tomultiplexrequestsandreduceconnectionoverhead.4)Limitsessionusagebyclosin

PHP Dependency Injection: Improve Code TestabilityPHP Dependency Injection: Improve Code TestabilityMay 12, 2025 am 12:03 AM

Dependency injection (DI) significantly improves the testability of PHP code by explicitly transitive dependencies. 1) DI decoupling classes and specific implementations make testing and maintenance more flexible. 2) Among the three types, the constructor injects explicit expression dependencies to keep the state consistent. 3) Use DI containers to manage complex dependencies to improve code quality and development efficiency.

PHP Performance Optimization: Database Query OptimizationPHP Performance Optimization: Database Query OptimizationMay 12, 2025 am 12:02 AM

DatabasequeryoptimizationinPHPinvolvesseveralstrategiestoenhanceperformance.1)Selectonlynecessarycolumnstoreducedatatransfer.2)Useindexingtospeedupdataretrieval.3)Implementquerycachingtostoreresultsoffrequentqueries.4)Utilizepreparedstatementsforeffi

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.