php custom exceptions

微笑、不失礼 提交于 2019-12-06 00:42:27

问题


I was wondering how would one go about writing custom exception handlers.

so that I can do something like

throw new dbException($sql, $message);

and have it output

There was an error in your query Message: {$message here}
Query: {$sql here}
Line: {line exception was thrown on}
File: {file exception was thrown from}

but I also want to to catch eg syntax errors and parse errors (if possible)


回答1:


Well, you can extend the Exception class however you like. For custom exceptions, you might want to check out the post:

  • PHP 5 OOP: Delegation and Custom Exceptions

You should also find this thread useful:

  • Custom Exception Messages: Best practices



回答2:


Unless I am misunderstanding your question, you should be able to extend PHP's Exception class.




回答3:


Why don't use just write your own exception class derived from the standard base exception? See extending exceptions manual.



来源:https://stackoverflow.com/questions/5008965/php-custom-exceptions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!