locked
where to catch exceptions? RRS feed

  • Question

  • User379720387 posted

    I am getting a distance between two addresses from the Bing Maps api.  The interface with the api is contained within several files.

    When an address is bad an exception is thrown and logged with Elmah.

    Potentially I have 3 areas where I could put my try catch block and the ErrorSignal to Elmah. But where is best?

    The process starts with:

    newdistance = GetDistance(startaddress, endaddress);

    wrap this in a try catch?

    On that same page in a function:

    public static double GetDistance(string startaddress, string endaddress)
                {
                try
                    {
                    string locationsRequest = DistHandler.CreateRequest(startaddress, endaddress, "&avoid=minimizeTolls");
                    Response locationsResponse = DistHandler.MakeRequest(locationsRequest);
                    var rtold = DistHandler.ProcessResponse2(locationsResponse);
                    return rtold.TravelDistance;
                    }
                catch (Exception ex)
                    {
                    here?
                    }
                }

    In DistHandler:

    public static Response MakeRequest(string requestUrl)
            {
                try
                {
                    HttpWebRequest request = WebRequest.Create(requestUrl) as HttpWebRequest;
                    using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
                    {
                        if (response.StatusCode != HttpStatusCode.OK)
                            throw new Exception(String.Format(
                            "Server error (HTTP {0}: {1}).",
                            response.StatusCode,
                            response.StatusDescription));
                        DataContractJsonSerializer jsonSerializer = new DataContractJsonSerializer(typeof(Response));
                        object objResponse = jsonSerializer.ReadObject(response.GetResponseStream());
                        Response jsonResponse = objResponse as Response;
                        return jsonResponse;
                    }
                }
                catch (Exception e)
                {
                    here?
                }
            }

    Sunday, December 28, 2014 9:48 PM

Answers

  • User-821857111 posted

    Catch the bad address when the user enters it?
    No. Catch the exception that you get if the API says the address is bad.

    I was wondering if a try catch block wrapped around the 1st code block up above, would work if the actual exception occurs in a piece of code that that is called in MakeRequest, pretty much a different file.

    Yes. The exception will bubble up to the calling code.

    Also, how does one find out in advance what possible exceptions can be returned from a SendMail, or a third party api?

    Look at the documentation.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, December 29, 2014 2:27 PM

All replies

  • User197322208 posted

    Usually I catch in the GUI layer.

    If I need a custom exception, then I catch into the Business Logic Layer

    Sunday, December 28, 2014 11:20 PM
  • User-821857111 posted

    catch (Exception ex) { here? }

    Yes. You should make sure you return a value to the UI so that the user can be informed that the address was bad. You might also consider catching different types of exceptions so that you can distinguish between a bad address and a network problem that prevents the API from being reached.

    Monday, December 29, 2014 4:56 AM
  • User379720387 posted

    Catch the bad address when the user enters it?

    Currently I have no means of doing that, I am left to let the distance api query fail and then clean up afterwards.

    I was wondering if a try catch block wrapped around the 1st code block up above, would work if the actual exception occurs in a piece of code that that is called in MakeRequest, pretty much a different file.

    Also, how does one find out in advance what possible exceptions can be returned from a SendMail, or a third party api?

    Monday, December 29, 2014 10:40 AM
  • User-821857111 posted

    Catch the bad address when the user enters it?
    No. Catch the exception that you get if the API says the address is bad.

    I was wondering if a try catch block wrapped around the 1st code block up above, would work if the actual exception occurs in a piece of code that that is called in MakeRequest, pretty much a different file.

    Yes. The exception will bubble up to the calling code.

    Also, how does one find out in advance what possible exceptions can be returned from a SendMail, or a third party api?

    Look at the documentation.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, December 29, 2014 2:27 PM