Question How to handle exception, for "up to 10" limit, for integer (method parameter)

euoukos

New member
Joined
Feb 7, 2017
Messages
1
Programming Experience
10+
How to handle exception, for "up to 10" limit, for integer (method parameter)

Hello!
Recently I had an interview, where I was asked to handle up to 10 numberOfmyTEST.
I wrote the following approach, which was not the accepted one.
Could you please help me to understand how Could I handle this exception?


thank you!

C#:
using System;
using System.Collections.Generic;


public class myTEST
{
    public static int myMethod(int numberOfmyTEST)
    {
        if (numberOfmyTEST>10)
		{
            throw new NotImplementedException ("Up to 10 story buildings");
        }
		else
		{
		   // do something
		} 
	}


    public static void Main(String[] args)
    {
        Console.WriteLine(myMethod(3));
    }
}
 
You haven't told us what the specific instructions were but, if you were supposed to throw an exception in that case, you're throwing the wrong type of exception. NotImplementedException is NEVER thrown in production code.* It's what you throw when you have declared a method but have yet to add any code to it. Generally speaking, you'd use it only when you want to declare a method so that you can write code that calls it but you haven't got around to implementing it yet and you want to make sure that you don't forget, which might happen if you just leave it empty.

In your case, you should be throwing an ArgumentOutOfRangeException, which is for when an argument"s value falls outside a specific range of values that are valid for a parameter. The error message should generally specify what the valid range is, e.g.*"'numberOfmyTEST' must not he greater than 10.".
 
Back
Top Bottom