I actually think that I got it but need some additional confirmation from you because I want to understand it and do it the correct way! I still haven't found a straight line in abstracting exception handling and logical code. So I try to get closer to it with your help.
Let's consider I call the function "foo(0)":
// Code 1
int foo(int a)
{
int value = 10;
return value/a;
}
This will throw "division by zero" exception.
But if parameter "a" must be inside of a specific range for function "bar" (Code 2) to be able to return a valid result (e.g. in the range of [5...10]), calling it with a value outside this range (e.g. 3) would of course not throw an exception unless I define one. So for this situation I define an exception, don't I?
For example this way:
// Code 2
void bar(int b)
{
if (b < 5)
{
throw new ArgumentException("Your input parameter is below minimum acceptable value");
}
else if (b > 10)
{
throw new ArgumentException("Your input parameter is above maximum acceptable value");
}
else
{
output(b);
}
}
or shouldn't exceptions be used for that (well I think they're here for exactly this purpose) and I rather do it this way?
// Code 3
int bar(int b)
{
int error = 0
if (b < 5)
{
error = -1;
}
else if (b > 10)
{
error = -2;
}
else
{
output(b);
}
return error;
}
Thx for your input(s).
Cheers
(The reason why I ask is because I do have a source code of a released software in front of me that has both. I have not much experience in OOP, consequently not either with "try-catch". And, believing the post found and the youtube tutorials I went through, this topic is very misunderstood. And I guess the developer of the software in front of me did misunderstand it. Tell me if I am wrong.)