开发者

Good Programming Practice regarding Exception Handling

Consider a function hierarchy, Function Four() Calls Three() which calls Two() Which again calls One() to do the job:

Function One($x) {
  if(!is_int($x)) {
     throw Exception("X must be integer");
  }
  // .......... Do the Job ................
}
Function Two($x) {
  if(!is_int($x)) {
     throw Exception("X must be integer");
  } else {
     One($x);
  }
}
Function Three($x) {
  if(!is_int($x)) {
     t开发者_开发技巧hrow Exception("X must be integer");
  } else {
     Two($x);
  }
}
Function Four($x) {
  if(!is_int($x)) {
     throw Exception("X must be integer");
  } else {
     Three($x);
  }
}

If I call four with a String value it will cause an Exception to occure.

Now consider following code with Exception in parent function only.

Function One($x) {
  if(!is_int($x)) {
     throw Exception("X must be integer");
  }
  // .......... Do the Job ................
}
Function Two($x) {
     One($x);
}
Function Three($x) {
     Two($x);
}
Function Four($x) {
     Three($x);
}

Here, I call Four() and pass a string, it will also cause an Exception to occure.

So which one is the best practice and why?

When I start writing code I end up writing a lot of exception handling, plz help.


If the functionality in function one needs $x and that is the only function that uses $x you can throw the exception only in function one. I assume function two, three and four will also do other things (otherwise they are useless). In that case you should check the value also in those functions. If they do not already use $x they may do in the future. In that case it is easy to forget to check the $x value and a bug is born.


IME it's good practice to fail as close to the problem as possible. That being said, I think option 1 is the way to go. This makes it so that when the exception occurs, you have confidence that it was the call to Four that was the problem and you can debug from there. Option 2 is easy but you don't know if the problem lies in Four, Three, Two or One. As your software grows in complexity so will your time debugging.


an alternative, i would know the trace and you can use as

Function One($x) {
  if(!is_int($x)) {
     throw Exception("X must be integer");
  }
  // .......... Do the Job ................
}
Function Two($x) {
    try
    {
        One($x);
    }
    catch(Exception $e)
    {
        throw $e;
    }
}
Function Three($x) {
    try
    {
         Two($x);
     }
    catch(Exception $e)
    {
        throw $e;
    }
}
Function Four($x) {
    try
    {
     Three($x);
     }
    catch(Exception $e)
    {
        throw $e;
    }

}

in this case you can know where is the error start, and only 1 is_int check.


I think it depends largely on what the functions are meant to do.

If the value of x is critical to the function, then the exception should be handled in that function. If it is just used for passing parameters, then you can let it slip through to the last function.

I am not sure of how the engine evaluates the function processing, but mostly it will push its contents onto the stack and then make the necessary jump, which in turn causes delay, so better to incorporate the exception handling then and there, when the values are generated, rather than letting it flow uselessly till it is being used just to save useless processing time.

Generally an exception means you have wasteful computation. So model 1 will make sure you don't do so.

Server Processing is precious to all... :)


go here and you may find the answer:

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜