Given the following code, how can I return some value (DateTime.Now.ToString()) if the exception is thrown?
public string DateToString(int year, int month, int day)
{
try
{
DateTime datetime = new DateTime(year, month, day);
return datetime.ToString();
}
catch (Exception)
{
//log error and rethrow
throw;
}
}
When you throw an exception, your method ends immediately.
There is no way to return a value too.
When you call a method which throws an exception, control will immediately transfer to a catch
block.
You won't get any chance to observe or use the (nonexistent-) return value.
You should rethink your design.
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