What exception to throw?

This question may seem a little silly, but here it goes.

I have two functions that can be called at any time. The first function takes a snapshot, and the second analyzes the data taken from this snapshot. Of course, if a user tries to parse a snapshot before taking it, my application should throw an exception. I know ArgumentOutOfRangeExceptionwhich is usually called when ...... there is an invalid argument, but actually it is not. Is there a built-in exception for such cases, or will I have to use ArgumentOutOfRangeException?

thanks

+3
source share
4 answers

Sounds like an InvalidOperationException. http://msdn.microsoft.com/en-us/library/system.invalidoperationexception.aspx

However, if you can design your API so that you cannot get into this situation, it would be better. Something like (pseudo):

public Data TakeSnapshot()
{
   // ...
   return new Data(...);
}

public void Analyze(Data data)
{
   // ...
}

Thus, there is no way to cause them to fail.

+16
source

?

 ISnapshot getSnapshot()

ISnapshot, . (), ,

+8
0

Source: https://habr.com/ru/post/1714849/


All Articles