|
-
Oct 24th, 2002, 03:08 PM
#1
Thread Starter
yay gay
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;
}
}
-
Oct 24th, 2002, 03:10 PM
#2
Thread Starter
yay gay
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
-
Oct 24th, 2002, 05:09 PM
#3
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")
-
Oct 24th, 2002, 05:26 PM
#4
PowerPoster
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.
-
Oct 24th, 2002, 06:12 PM
#5
Thread Starter
yay gay
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?
-
Oct 24th, 2002, 06:21 PM
#6
PowerPoster
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;
}
-
Oct 24th, 2002, 06:33 PM
#7
Thread Starter
yay gay
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
-
Oct 24th, 2002, 06:37 PM
#8
PowerPoster
Sure, remeber that all .NET Languages use the same framework, thus the same objects.
Code:
catch (Exception e)
{
return "err: " + e.Message;
}
-
Oct 24th, 2002, 07:31 PM
#9
PowerPoster
You don't even need to specify an exception type if you don't want to:
[code]
try
{
// code here
}
catch
{
// Catches any error
}
-
Oct 24th, 2002, 07:35 PM
#10
PowerPoster
good 2 know...
-
Oct 25th, 2002, 08:20 AM
#11
Thread Starter
yay gay
-
Oct 26th, 2002, 01:13 PM
#12
PowerPoster
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.
-
Oct 26th, 2002, 05:46 PM
#13
PowerPoster
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.
-
Oct 26th, 2002, 05:48 PM
#14
PowerPoster
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|