Question 1
public class foo { }
public foo SomeFooNULL()
{
var newFoo = new foo();
if(null == newFoo)
{
return null;
}
return newFoo;
}
public foo SomeFooException()
{
var newFoo = new foo();
if (null == newFoo)
{
throw new NullReferenceException();
}
return newFoo;
}
In the case of C, C++, which was previously encountered, it usually returns the same way as the SomeFooNULL function and writes defense codes such as NULL CHECK on the caller, but recently encountered C#, you can see example codes written in the following way. When there is a code like above, I wonder which method is recommended by C# or which method is used a lot.
Question 2
public enum FOO_RESULT
{
OK = 0,
ERROR = 1,
FOO = 2,
BAR = 3
}
public FOO_RESULT DoSomeEnum()
{
if(true /* arbitrary error check1*/)
return FOO_RESULT.ERROR;
if (true /* arbitrary error check 2*/)
return FOO_RESULT.FOO;
/* Any action */
return FOO_RESULT.OK;
}
[Serializable]
public class MyException : Exception
{
public MyException(string message) : base(message) { }
}
public void DoSomeException()
{
if (true /* arbitrary error check 1*/)
throw new MyException("ERROR");
if (true /* arbitrary error check 2*/)
throw new MyException("FOO");
/* Any action */
}
As an extension of the above question, the traditional method, like the DoSomeEnum function above, defines Enum and returns it to the caller by returning it, but in C#, you can also see example codes for defining UserException directly and throwing exceptions, and I wonder what method is recommended.
Additionally, the exception in C++ is very slow, and I wonder if you are concerned about the performance degradation of C#'s exception. I've been asking questions for a long time, so please take a step forward.
exception exception-handling c#
The error code and exception are long and short, respectively.
In the case of exception, performance of very performance-sensitive applications (middleware such as DB) is clearly compromised, but otherwise it's not a big deal.
If the error code is used, it is very difficult to forward the error to the top with the stack deep.(You must forward the error code one step at a time to the top, which is difficult to process on the caller's side.)
With Exception, you can do a stack jump that you cannot get with error code. This allows you to easily collect and process error logs and other exception handling in one place.
Therefore, when looking at web frameworks, etc., the entry point that the user requests is set as Try/Catch and consistently handles exceptions/errors.
For your information, the exception itself is not very slow.
If the exception is deep and the exception is handled too high, performance is a problem (because it rolls up the stack information), and if this is a serious problem, you can override that behavior to prevent performance degradation.
Of course, detailed stack information will be lost at this time, but the control flow will remain accurate.
It depends on the situation. I think you only need to use throw when you think it's an error.
© 2024 OneMinuteCode. All rights reserved.