If you need to return multiple values from the function, then:
1 - Define a return type
public class MyReturnResult
{
public bool IsException;
public string Message;
}
public MyReturnResult MyMethod()
{
try
{
...
}
catch (Exception e)
{
return new MyReturnResult() {IsException = true, Message = e.ToString()};
}
// can be inside try
return new MyReturnResult() {IsException = false, Message = "Ok"};
}
2 - Use famous GetLastError theme
public string LastMessage;
public bool MyMethod()
{
try
{
...
}
catch (Exception e)
{
LastMessage = e.ToString();
return true;
}
LastMessage = "Ok";
return false; // no exception
}
3 - Use out
parameters
// or make bool function and string out parameter
public string MyMethod(out bool isException)
{
try
{
...
}
catch (Exception e)
{
isException = true;
return e.ToString();
}
isException = false;
return "Ok";
}
In your case you may define special string (to example, "blablablaOk"
or with null
value) to indicate no exception case, otherwise it would indicate what there was exception and would contain different from that exception message.
public string MyMethod()
{
try
{
...
}
catch (Exception e)
{
return e.ToString();
}
return "blablablaOk";
}
and then you check
if(MyMethod() == "blablablaOk")
{
// no exception
}
else
{
}