php異常處理方法執行個體匯總_PHP教程

來源:互聯網
上載者:User

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程式設計有所協助。

http://www.bkjia.com/PHPjc/1020889.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/1020889.htmlTechArticlephp異常處理方法執行個體匯總 本文執行個體講述了php異常處理方法。分享給大家供大家參考。具體如下: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    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.