Results 1 to 14 of 14

Thread: catching...

  1. #1

    Thread Starter
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729

    catching...

    this code isnt working:

    PHP Code:
            private string downloadData(string uRL)
            {
                
    byte[] ssBuffer;
                
    WebClient wcClient = new WebClient();

                try
                {
                    
    ssBuffer wcClient.DownloadData(uRL);
                    return 
    Encoding.Default.GetString(ssBuffer);
                }

                catch (
    InvalidCastException e)
                {
                    return 
    "err: " e.Message;
                }

            } 

  2. #2

    Thread Starter
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    i mean...im getting an error in:

    PHP Code:
    ssBuffer wcClient.DownloadData(uRL); 
    and the expcetion isnt being used...why?!?

    btw im doin the error just for testing porpose... im testing C# lol

  3. #3
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Are you getting an exception in during the assignment of the function result instead in the function itself?

    I mean it could be complaining about the app part:

    interger x=downloadData("http://www.vbforums.com")

  4. #4
    PowerPoster Lethal's Avatar
    Join Date
    Oct 2000
    Location
    Ohio
    Posts
    2,496
    Here you go, you must also verify that a well-formed uri is passed in:

    Code:
    using System;
    using System.Net;
    using System.Text;
    
    public class DownloadURL
    {
    	public string downloadData(string uRL)
    	{
    		WebClient wcClient = new WebClient();
    		string download;
    		const string prefix = "http://";
    
    		try
    		{
    			if  (!uRL.ToLower().StartsWith("http://"))
    			{
    				uRL = prefix + uRL;
    			}
    
    			byte[] myDataBuffer = wcClient.DownloadData (uRL);
    			download = Encoding.ASCII.GetString(myDataBuffer);
    			return download;
    			
    		}
    
    		catch (WebException  e)
    
    		{
    			return "err: " + e.Message;
    		}
    
    	}
    }
    
    public class Test
    {
    	public static void Main()
    	{
    		string data;
    		DownloadURL myDownload = new DownloadURL();
    		data = myDownload.downloadData("http://www.vbworld.com");
    		Console.WriteLine(data);
    	}
    }
    Last edited by Lethal; Oct 24th, 2002 at 06:24 PM.

  5. #5

    Thread Starter
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    no! u dont getting the point..my function works just fine...but i now i did put a WRONG URL so my catch will get into a bug...i did this just to check if the catch was working ok...but it isnt! instead of returning a string saying "err: " + e.message i get a IDE error message...what im doin wrong?

  6. #6
    PowerPoster Lethal's Avatar
    Join Date
    Oct 2000
    Location
    Ohio
    Posts
    2,496
    Lets think about this....You are trying to catch a cast exception. This is not the exception thrown! Calm down bro, only trying to help...

    Code:
    catch (WebException  e)
    {
           return "err: " + e.Message;
    }

  7. #7

    Thread Starter
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    loll im calm bro lolol look hmm in VB.NET u put

    catch err as exception
    (code)

    hmm....isnt there something like that in C# who handles all king of bugs? lol

  8. #8
    PowerPoster Lethal's Avatar
    Join Date
    Oct 2000
    Location
    Ohio
    Posts
    2,496
    Sure, remeber that all .NET Languages use the same framework, thus the same objects.

    Code:
    catch (Exception e)
    {
    	return "err: " + e.Message;
    }

  9. #9
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    You don't even need to specify an exception type if you don't want to:
    [code]
    try
    {
    // code here
    }
    catch
    {
    // Catches any error
    }

  10. #10
    PowerPoster Lethal's Avatar
    Join Date
    Oct 2000
    Location
    Ohio
    Posts
    2,496
    good 2 know...

  11. #11

    Thread Starter
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    yea tks

  12. #12
    PowerPoster Lethal's Avatar
    Join Date
    Oct 2000
    Location
    Ohio
    Posts
    2,496
    The only downside is that without catching at least a generic exception object, we do not have access to any of the exception properties in case we would like to display any additional info.

  13. #13
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    Your right, in the case you do need to catch any exception and get some info from it, you can just use the System.Exception type. It will still catch any exception thrown by the framework. Not sure though if it will catch custom exceptions. It would if the custom exception used the interfaces and such....hmmm...something to look into.

  14. #14
    PowerPoster Lethal's Avatar
    Join Date
    Oct 2000
    Location
    Ohio
    Posts
    2,496
    Yep, it will catch the custom exception b/c all exception classes are derived from the base System.Exception. However, we use the System.ApplicationException base class when writing our own custom exceptions, but this is also derived from System.Exception.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width