Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The handling method of PHP exception class

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains the "handling methods of PHP exception class". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "the handling method of PHP exception class".

PHP has booked two exception classes: Exception and ErrorException

The copy code is as follows:

Exception {

/ * attribute * /

Protected string $message; / / exception message content

Protected int $code; / / exception code number

Protected string $file; / / the file name that throws the exception

Protected int $line; / / throw the line number of the exception in the file

/ * method * /

Public _ _ construct ([string $message = "" [, int $code = 0 [, Exception $previous = null])

Final public string getMessage (void) / / Information thrown by exception

Final public Exception getPrevious (void) / / previous exception

Final public int getCode (void) / / exception code, which is user-defined

Final public string getFile (void) / / abnormal file Lujin

Final public int getLine (void) / / the line where the exception occurred

Final public array getTrace (void) / / exception tracking Information (array)

Final public string getTraceAsString (void) / / exception tracking Information (string)

Public string _ _ toString (void) / / call the return value of the subfunction when attempting to directly use the exception object as a string

Called when final private void _ _ clone (void) / / when an exception object is cloned

}

The copy code is as follows:

ErrorException extends Exception {

/ * attribute * /

Protected int $severity

/ * method * /

Public _ _ construct ([string $message = "" [, int $code = 0 [, int $severity = 1 [, string $filename = _ _ FILE__ [, int $lineno = _ LINE__ [, Exception $previous = NULL])

Final public int getSeverity (void)

/ * inheritance method * /

Final public string Exception::getMessage (void)

Final public Exception Exception::getPrevious (void)

Final public int Exception::getCode (void)

Final public string Exception::getFile (void)

Final public int Exception::getLine (void)

Final public array Exception::getTrace (void)

Final public string Exception::getTraceAsString (void)

Public string Exception::__toString (void)

Final private void Exception::__clone (void)

}

So how do you catch an exception?

(1) available try...catch... for PHP The exception is caught and the code that handles the exception must be within the try code block.

The copy code is as follows:

Try {

Throw new Exception ('exception test 1 million, 1001)

} catch (Exception $e) {

Echo $e-> getMessage ().'-'.$ e-> getCode ()

}

(2) you can customize the exception handler [set_exception_handler] for exceptions that are not caught with try/catch.

The copy code is as follows:

Function exception_handler ($e) {

Echo "Uncaught exception:", $e-> getMessage (), "\ n"

}

Set_exception_handler ('exception_handler')

Throw new Exception ('Uncaught Exception')

Echo, "this line will not be implemented."

You can see that using the ser_exception_handler callback function to handle exceptions, subsequent code will not continue to execute, but try-catch can.

(3) PHP can catch different types of exceptions with multiple catch and allow exceptions to be thrown again within the catch code block.

The copy code is as follows:

/ / Please expand the exception class according to the actual situation

Class MyException extends Exception {

Public function _ _ construct ($message ='', $code = 0) {

}

Public function myFunction () {

Echo 'just for test'

}

}

Try {

Throw new MyException ('an error')

} catch (MyException $e) {

Echo $e-> myFunction ()

} catch (Exception $e) {

Echo $e-> getMessage ()

}

(4) PHP5.5 already supports the finally keyword, so you don't have to worry about whether the exception has overflowed.

The comparisons are as follows:

The copy code is as follows:

Function doSomething () {

$resource = createResource ()

Try {

$result = useResource ($resource)

} catch (Exception $e) {

ReleaseResource ($resource)

Log ($e-> getMessage ())

Exit ()

}

ReleaseResource ($resource)

Return $result

}

/ / after using finally

Function doSomething2 () {

$resource = createResource ()

Try {

$result = useResource ($resource)

Return $result

} catch (Exception $e) {

Log ($e-> getMessage ())

Exit ()

} finally {

ReleaseResource ($resource)

}

}

Thank you for your reading, the above is the content of "handling methods of PHP exception classes". After the study of this article, I believe you have a deeper understanding of the handling methods of PHP exception classes, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report