Hertzole
Hertzole
CC#
Created by Hertzole on 12/19/2023 in #help
Use exceptions or error enum?
Hi there I'm currently designing a C# wrapper API for a web API and I'm not entirely sure how I should treat errors. My wrapper basically returns responses in neat structs that the end user then can use however they please. But should possible errors be a part of that struct? Some pseudo code to hopefully explain what options I have
MyResponse response = await Serializer.Deserialize(json);
if(response.message == "Invalid Credentials")
{
// A:
throw new MyAuthException("Invalid credentials");
// B:
return new MyResponse(success: false, errror: MyErrors.InvalidCredentials);
}
MyResponse response = await Serializer.Deserialize(json);
if(response.message == "Invalid Credentials")
{
// A:
throw new MyAuthException("Invalid credentials");
// B:
return new MyResponse(success: false, errror: MyErrors.InvalidCredentials);
}
I'm just not sure what is the most desired for the average .NET developer. I've heard exceptions have some performance overhead when throwing. The library is also designed to be used in Unity and from my experience in Unity, you really don't catch exceptions as often.
8 replies