Exception Handling - is this good practice?

bartosz.baczek

I am wondering if what i am going to do is good or bad thing. I have that class:

public class Element : IElement
{
    public float? Max { get; private set; }

    public float? Min { get; private set; }

    public float? Average { get; private set; }

    public bool HasValue { get; private set; }


    public void SetRange(float? min, float? max)
    {
        if (min >= max)
        {
           throw new WrongElementValueException("Min must be greater than max!");
        }
        else if (min < 0f || max < 0f)
        {
           throw new WrongElementValueException("Min/max must be greater than 0!");
        }
        else if (min > 100f || max > 100f)
        {
           throw new WrongElementValueException("Min/max must be lesser than 0!");
        }
        else
        {
            Min = min;
            Max = max;
            Average = (min + max)/2f;
            HasValue = true;
        }
    }
}

The user will set the values using SetRange() method. But he has some constraints like Min must be bigger than Max, and neither of them should be bigger than 100 or lesser than 0.

Should I use those exceptions in this place? Or is there any better method to handle wrong user input? I hope my question isn't to general.

Jon Hanna
else if (min < 0f || max < 0f)
  throw new WrongElementValueException("Min/max must be greater than 0!");
else if (min > 100f || max > 100f)
  throw new WrongElementValueException("Min/max must be lesser than 0!");

I'd note that there is already ArgumentOutOfRangeException that's defined for precisely this sort of case.

if (min >= max)
  throw new WrongElementValueException("Min must be greater than max!");

This should definitely be an ArgumentException, but if WrongElementValueException inherits from ArgumentException, then that's fine.

Your general approach is sound. I'd consider going further:

HasValue = true;

Why allow for the class to ever not have a value. Consider if you add:

public Element(float min, float max)
{
  SetRange(min, max);
}

Now you can never have an instance without its values set, and can get rid of HasValue entirely.

Note though that I changed this from float? to float. You might well be advised to do that throughout the class. Otherwise if you have a need for cases where Min and Max are null, (and therefore don't want to get rid of HasValue) you need to catch that case in SetRange:

public void SetRange(float? min, float? max)
{
    if (min < 0f || max < 0f || min > 100f || max > 100f)
      throw new ArgumentOutOfRangeException();
    if (min >= max)
      throw new WrongElementValueException("Min must be greater than max!");
    Min = min;
    Max = max;
    if(min.HasValue && max.HasValue)
    {
        Average = (min + max)/2f;
        HasValue = true;
    }
    else
    {
      Average = null;
      HasValue = false;
    }
}

(I'd also generally favour double and double? over float and float? unless you've a strong reason otherwise).

Incidentally, we generally use "exception handling" to talk about how code that's using this code deals with the fact that your code threw an exception, with just what the best thing to do is depending on the context of that code rather than this code.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Good practice design pattern for Exception handling

Best practice for Java exception handling

Android exception handling best practice?

Exception Handling Best-Practice

Is handling all possible (unchecked) Exceptions good practice?

What is Good Practice 'Error Handling' for Swift 4?

If catching null pointer exception is not a good practice, is catching exception a good one?

Is it a good practice to log error inside exception constructor?

Is it a good practice to cache exception instance in C#

Is it a good practice to throw Exception inside setters in Java?

Good practice of exception hierarchy in C#

Is it a good practice to parameterize error message in exception constructor?

Node.js Best Practice Exception Handling

Good practice for error handling and cleaning up resources in SDL?

Is it good practice to throw exception from DAO layer to controller?

Is it good practice to catch exception or do a null check for Arrays.copyOf

Is it a good practice to close an SQL connection after catching an Exception?

is it good practice to call a method from Exception block in java?

Is this a good practice?

Is this good practice?

Django - exception handling best practice and sending customized error message

How using try catch for exception handling is best practice

Node.js Best Practice Exception Handling - After Async/Await

Is it good practice to have Generic Exception class handler in global exception handler with spring rest?

In Java, is using throws Exception instead of throwing multiple specific exceptions good practice?

Is it good practice to use Exception to prevent class from being instantiated with unwanted parameter

Is it a good practice to have specific Exceptions for everything? Or is it better to reuse a bit more abstract exception?

Is it a good practice to add a very small number to the divisor not to deal with division by zero exception?

EntityManager inject good practice