0

Hey,

During my development of web browser i have encountered a problem. The problem is that on my bookmarks panel if a user adds a bookmark that's already on the panel, the application gets an error and has to quit.

I tried surrounding the code with a try catch statement but then i get another error! This is the error

Invalid URI: The format of the URI could not be determined.

Here is my code;

private int faviconIndex(string url)
        {
            try
            {
                Uri key = new Uri(url);
                if (!imgList.Images.ContainsKey(key.Host.ToString()))
                    imgList.Images.Add(key.Host.ToString(), favicon(url, "link.png"));

                Uri keys = new Uri(url);
                return imgList.Images.IndexOfKey(keys.Host.ToString());
            }
            catch
            {
                Uri keyi = new Uri(url);
                return   imgList.Images.IndexOfKey(keyi.Host.ToString());
            }
            
        }

When i take away the try/catch statement or the

return   imgList.Images.IndexOfKey(keyi.Host.ToString());

It works fine except for the error when someone else adds the same bookmark.

Any help is much appreciated,

NSSLTD

4
Contributors
3
Replies
4
Views
6 Years
Discussion Span
Last Post by Mitja Bonca
Featured Replies
  • Hi, The null input should always be verified before proceeding with with controls. otherwise unexpected exception may be throws by the UI. Try this code instead. [CODE] private int faviconIndex(string url) { try { // checks if input param is null if (url != null) { //checks if string is … Read More

1

Hi,

The null input should always be verified before proceeding with with controls. otherwise unexpected exception may be throws by the UI. Try this code instead.

private int faviconIndex(string url)
    {
        try
        {
            // checks if input param is null
            if (url != null)
            {
                //checks if string is empty
                if (url != string.Empty)
                {
                    // if verification is good, then proceed.
                    Uri key = new Uri(url);

                    if (imgList.Images.ContainsKey(key.Host.ToString()))
                    {     
                        return imgList.Images.IndexOfKey(keys.Host.ToString());
                    }
                    else
                    {
                        // if does not exist, then add the element into the control.
                        // and return the index.
                        imgList.Images.Add(key.Host.ToString(), favicon(url, "link.png"));
                        return imgList.Images.IndexOfKey(key.Host.ToString());
                    }
                }
            }
        }
        catch
        {
            // verify in your caller procedure if, -1 is retuned, fucntion failure.
            // do not process the control.
            // or u can throw an exception if you want
            return -1;
        }
        // unanticipated error. if input is null or empty.
        return -2;
    }
Comments
Thank you it really helped and you took the time to modify my code to suit my needs. Much appreciated!
0

You can simplify the url null test if you use if (!String.IsNullOrEmpty(url)) Or if (!String.IsNullOrWhiteSpace(url))

Edited by nick.crane: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.