I trying to improve my skills using Try Catch blocks and better error handling.
I have a class that performs a common task, in this case retrieving a Facebook AccessToken. If successful, I want to return the AccessToken string, if not I want to return an error message. These are both strings, so no problem. But when checking the return value on the calling side of the code, how can you do this effectively?
It's like I need to return 2 values. In the case of a successful attempt, return = true, "ACESSCODEACXDJGKEIDJ", or if it fails, return = false, "Ooops, there was an error" + ex.ToString();
Then checking the return value is easy (in theory). I could think of returning simply a true/false for return and then setting a Session variable for the strings.
What is a way to return multiple results from a method?
You can return only one value in Java. If needed you can return multiple values using array or an object.
If we want the function to return multiple values of same data types, we could return the pointer to array of that data types. We can also make the function return multiple values by using the arguments of the function.
Below are the methods to return multiple values from a function in C: By using pointers. By using structures. By using Arrays.
You cannot explicitly return two variables from a single function, but there are various ways you could concatenate the two variables in order to return them.
Create a Result class and return that instead...
public class Result
{
public bool Success {get;set;}
public string AccessToken {get;set;}
public string ErrorMessage {get;set;}
}
public Result GetFacebookToken()
{
Result result = new Result();
try{
result.AccessToken = "FACEBOOK TOKEN";
result.Success = true;
}
catch(Exception ex){
result.ErrorMessage = ex.Message;
result.Success = false;
}
return result;
}
Then you can call this code like...
Result result = GetFacebookToken();
if(result.Success)
{
//do something with result.AccessToken
}
else
{
//do something with result.ErrorMessage
}
2 possibilities spring to mind
Let's first see the TryXXX pattern. It's basically a method that returns a boolean value and the result as out
parameter.
public bool TryXXX(string someInput, out string someResult, out string errorMessage)
{
...
}
which will be consumed like this:
string someResult;
string errorMessage;
if (!TryXXX("some parameter", out someResult, out errorMessage))
{
// an error occurred => use errorMessage to get more details
}
else
{
// everything went fine => use the results here
}
In the second approach you would simply design a class that will contain all the necessary information:
public class MyResult
{
public bool Success { get; set; }
public string ErrorMessage { get; set; }
public string SomeResult { get; set; }
}
and then have your method return this class:
public MyResult MyMethod(string someParameter)
{
...
}
which will be consumed like this:
MyResult result = MyMethod("someParameter");
if (!result.Success)
{
// an error occurred => use result.ErrorMessage to get more details
}
else
{
// everything went fine => use the result.SomeResult here
}
Of course the results can be any other complex object instead of (as shown in this example) a string.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With