搜尋
首頁php教程php手册php异常处理方法实例汇总

php异常处理方法实例汇总

Jun 13, 2016 am 09:00 AM
php分享處理實例例外方法匯總

php异常处理方法实例汇总

          本文实例讲述了php异常处理方法。分享给大家供大家参考。具体如下:

?

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

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

}

}

?>

?

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

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

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; //重掷异常

}

}

?>

          希望本文所述对大家的php程序设计有所帮助。

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

將Eclipse與SAP NetWeaver應用伺服器整合。

VSCode Windows 64位元 下載

VSCode Windows 64位元 下載

微軟推出的免費、功能強大的一款IDE編輯器

SublimeText3 Linux新版

SublimeText3 Linux新版

SublimeText3 Linux最新版

mPDF

mPDF

mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具