简单异常示例-PHP
我试图了解在以下场景中处理异常的最佳方法: 我有一个班的员工:简单异常示例-PHP,php,oop,Php,Oop,我试图了解在以下场景中处理异常的最佳方法: 我有一个班的员工: class employee extends person { private $salary; private $baseSalary = 6.5; function __construct($f, $m, $l, $a,$fsalary=0){ if(!is_numeric($fsalary)){ throw new Exception("Age supplied
class employee extends person {
private $salary;
private $baseSalary = 6.5;
function __construct($f, $m, $l, $a,$fsalary=0){
if(!is_numeric($fsalary)){
throw new Exception("Age supplied is not a number", 114);
}
parent::__construct($f, $m, $l, $a);
$this->salary=$fsalary;
}
function GetDetails(){
return parent::GetName().
"<br/>".
$this->salary;
}
function __toString(){
return $this->GetDetails();
}
}
class雇员扩展个人{
私人收入$工资;
私人美元基本工资=6.5;
函数构造($f、$m、$l、$a、$fsalary=0){
如果(!是数字($fsalary)){
抛出新异常(“提供的年龄不是一个数字”,114);
}
父项::u构造($f、$m、$l、$a);
$this->salary=$fsalary;
}
函数GetDetails(){
返回父::GetName()。
“
”。
$this->salary;
}
函数u_toString(){
返回$this->GetDetails();
}
}
使用这个:
try{
if(!$f = new employee("Sarah", "Sebastian", "Pira", "abc")){
throw new Exception();
}
else {
echo $f;
}
}
catch (Exception $e){
echo "<br/>";
echo var_dump($e);
}
试试看{
如果(!$f=新员工(“莎拉”、“塞巴斯蒂安”、“皮拉”、“abc”)){
抛出新异常();
}
否则{
echo$f;
}
}
捕获(例外$e){
回声“
”;
echo var_dump($e);
}
现在我认为在类中抛出一个异常,然后在所有将使用employee对象的脚本中只使用一个catch块是一个好主意-但是这似乎不起作用-我需要在类中有一个try-catch块-这是正确的方法吗
谢谢我想你说的是你想做这样的事情:
try {
class Employee extends Person {
// ...blah blah...
}
}
catch(Exception $e) {
// handle exception
}
try {
$employee = new Employee();
$employee->doSomeStuff();
$employee->doMoreStuffThatCouldThrowExceptions();
}
catch(Exception $e) {
handle_employee_exception($e);
}
…然后能够在其他类中使其疯狂,而不显式捕获任何异常:
// try { << this would be removed
$employee = new Employee();
// }
// catch(Exception $e) {
// (a whole bunch of code to handle the exception here)
// }
try {
// some code here
} catch (EmployeeModule_Exception $e) {
// display information about exception caught
echo 'Error message: ' . $e->getMessage() . '<br />';
echo 'Error code: ' . $e->getCode();
}
它并没有消除每个文件中的try/catch块,但它确实意味着您不必一直重复异常处理的实现。不要将handle\u employee\u exception定义为类的实例方法,将其作为一个单独的函数来执行,否则如果在构造函数中抛出该异常,将导致致命错误,因为该变量不存在。您应该阅读更多有关该异常的信息 当然,您可以在类的方法中处理异常。但是你应该重新考虑你想怎么做,然后。。。为什么 良好的实践还包括创建自己的异常类,这样您就能够区分模块/类引发的异常和其他异常引发的异常。看起来是这样的(): 在抛出异常时:
// the second parameter below is error code
throw new EmployeeModule_Exception('some message', 123);
捕获与此类似,只有以下示例将仅捕获模块的异常:
// try { << this would be removed
$employee = new Employee();
// }
// catch(Exception $e) {
// (a whole bunch of code to handle the exception here)
// }
try {
// some code here
} catch (EmployeeModule_Exception $e) {
// display information about exception caught
echo 'Error message: ' . $e->getMessage() . '<br />';
echo 'Error code: ' . $e->getCode();
}
试试看{
//这里有一些代码
}捕获(员工模块_异常$e){
//显示有关捕获的异常的信息
回显“错误消息:”。$e->getMessage()。
;
回显“错误代码:”。$e->getCode();
}
哪个部件不工作?您希望在第二个代码块中发生什么?看起来不会抛出任何异常,它只会回显Sarah Sebastian Pira
0
对吗?如果employee对象在单独的脚本中使用,则在创建对象时不会触发类的catch块-我的问题是,是否可以在类中定义一个try-catch块,而不是在是否使用employee对象?当使用这种方法时,我得到一个致命错误,指出异常是未捕获的仅供参考:在PHP中,使用以小写字符开头的类名和以大写字符开头的方法名是一种非常不寻常的编码样式。通常,它是类Employee extends Person
和函数getDetails
。另一个提示:1)不要抛出空异常!2) 不要在条件范围内使用作业(除非你真的知道自己在做什么,并且了解它是如何工作的),谢谢正是我想要的