I have a web service that uses this 3rd party call(geoLock.AdjustSensorsAsync
) to create an object.
When it works, it works fine. Here it is:
geologicalEvent.LocationName = geologicalEvent.LocationName;
try
{
sensorResult = await geoLock.AdjustSensorsAsync(
geologicalEvent.LocationId,
geologicalEvent.LocationName,
geologicalEvent.LocationStart.ToUniversalTime(),
geologicalEvent.LocationEnd.ToUniversalTime()
);
} catch
{
}
But sometimes a bad LocationStart
or LocationEnd
is passed to it and I'll get an error like:
FaultException: "Unable to record. Invalid time."
Right now, it just shows the line number of the error, but I'd like to see more detailed info on what entity caused the error.
What can I add to the catch
to show which specific LocationId
and also the 'LocationStart' and 'LocationEnd' that caused it?
Thanks!
CodePudding user response:
geologicalEvent.LocationName = geologicalEvent.LocationName;
try
{
sensorResult = await geoLock.AdjustSensorsAsync(
geologicalEvent.LocationId,
geologicalEvent.LocationName,
geologicalEvent.LocationStart.ToUniversalTime(),
geologicalEvent.LocationEnd.ToUniversalTime()
);
}
catch (Exception e)
{
Console.WriteLine("{0} Exception caught.", e);
}
Something like that maybe? Using the 'catch (Exception e)' to do a console writeline and see what it spits out.
Reference: Microsoft documentation - try-catch
CodePudding user response:
You define the catch block for the specific exception type and encapsulate it in your own exception with the desired information. Something like this:
catch (FaultException ex)
{
throw new InvalidOperationException($"{ex.Message} LocationStart: {geologicalEvent.LocationStart} LocationEnd: {geologicalEvent.LocationEnd}", ex);
}