Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

What can I add to a catch statement to show the properties that caused an error?

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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!

>Solution :

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);
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading