开发者

Exceptions in validation

I currently have some code that delibratly throws an exception if the user sends me data that fails validation (see below). I like it because im sure any errors in the application are caught and handled. A am however worried the code being slow as throwing exceptions takes a lot of memory. Im also worried it might be "bad code". Whats your advice? Thanks

  public class BTAmendAppointmentRequest
    {
      开发者_开发技巧  public DataLayer.WebserviceMessage AddBTAmendAppointmentRequest(DataLayer.BTAmendAppointmentRequest req)
        {
            DataLayer.WebserviceMessage rsp = new DataLayer.WebserviceMessage();
            try
            {

                if (!String.IsNullOrEmpty(req.AppointmentReference))
                    req.AppointmentReference = req.AppointmentReference.Trim();

                if (req.OrderRequestID < 1 || string.IsNullOrEmpty(req.AppointmentReference))
                {
                    throw new Exception("Amend appointment failed, you must supply a valid appointment reference and order reference");
                }

        ...Do other stuff
            }
            catch (Exception ex)
            {
                rsp = new Service.WebserviceErrorMessage(ex);
            }


            return rsp;

        }

    }


If you are expecting these errors, you should return error messages to the user, not throw exceptions.

Reserve exceptions to exceptional situations.

Apart from being expensive, the meaning of an exception, the semantics are that of something exceptional having happened. Validation failing is not exceptional, it is expected.

Having said that, seeing as you are on a web service, an exception is a reasonable thing to do, assuming you also validate before the service call. It is reasonable since a web service can be called by anything - validation may not have happened, and such errors should be exceptional. Additionally, at least with .NET web services, web exceptions are probably the best way to communicate such things back to the client.


Exceptions should be considered as last resort error trap. They should be "exceptional". Data input errors are not exceptions - they are very common, expected events. You shoudl handle validation issues with validation controls or processes, that handle them - display an error message and do not let the processing continue.

Your other problem is that you cannot easily do full form validation if the first error you encounter throws an exception. If I was filling out a form where each error was separately highlighted, I would give up very quickly. You need to be able to validate and display ALL errors on a page, and not permit progress without validation succeeding.


I tend to agree with Oded in that exceptions should only be used for stuff you aren't expecting. The other way to look at it is with using an errors collection, you are able to validate a larger batch instead of throwing an exception on the first problem. This can be more usable for the person consuming your service.

In the case of web services, I would package the entire response in a custom response object, which features a return code. This allows you to have a return code of error, and then encapsulate an errors collection in the response object.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜