Exception vs. Validation

I just met a property installer that catches exceptions (all exceptions, I know this is bad, but it doesn't matter here), and only logs them. First of all, I think that through them this should be repeated; why wait for the collapse and research of the magazine when you can immediately understand something?

However, my main question is that I check for invalid date values, add a RuleViolation object to the ValidationRules object in my document, or throw an InvalidDate exception or just allow the CLR exception for me (invalid dates do not check for ranges except invalid dates and etc.)

+5
c #
source share
5 answers

Exceptions should occur whenever a method or a member of a class cannot perform any task that it is intended to perform.

So, for the property installer, if the setter cannot set the property, it should throw an exception.

As for whether you should catch and reconstruct it, the answer will be yes, but only if you need to handle the exception right away in the installer before passing its stack ... but registration is not a reason to do it. In general, you should implement end-to-end exception logging at a higher level where the exception will NOT be re-selected ... if you care about these end-to-end issues higher than somewhere in the stack, then no, definitely not catch and re-throw one and the same exception.

However, if you are writing a tool or framework library where you want your component clients to have a well-defined set of expected exceptions, and you have defined your own custom exceptions that your component will use for client code and which client components expect to see, then you may want to catch the exceptions thrown by the CLR and instead change your own custom exceptions. Always include the actual base exception in your own type of InnerException before passing it to the stack so that the data in it is accessible to any system that ultimately consumes it.

+2
source share

It depends on the specific task. If you are writing a library class that will be used as a component in other programs, and the class method contract says that it should only accept valid dates, then throwing an Exception will be fine.

If you accept user input and then expect that exception is bad practice. In this case, you must confirm the date yourself.

Exceptions are for exceptional cases and should not be part of your logic. This usually means that the programmer violated the contract.

+4
source share

I think it depends on where the date values ​​came from. If this comes from user input or some other source where it is possible to enter "invalid" dates, then checking will be a way. On the other hand, if there is no predictable reason why data values ​​may be invalid, then throwing an exception is appropriate.

+2
source share

It really depends on the logic of your application. Exceptions should really be thrown only for exceptional situations. For something like validation, it depends on the tolerance for invalid data.

When you create an application for interaction and the user can enter something, it is probably normal that the document is in an invalid state and you should disclose the verification information through the properties of the document class.

If you are processing prepared documents from a database or a log file, then this is probably not normal if the document is invalid and continues to work after it can lead to data corruption in the system. When this happens you must quit.

+1
source share

Catching and reintronting is the worst. Is it expensive to TRY if you are just going to make a difference? You can catch unhandled exceptions with global.asax if you need to register them.

From a validation point of view, from a network point of view, I always use regular expression validators for dates, these client and client servers, so I know when inside

if(Page.IsValid) 

block that my txtDate.Text is a valid date, so I am not checking because it is simply wasteful.

0
source share

All Articles