Click to See Complete Forum and Search --> : catching...
PT Exorcist
Oct 24th, 2002, 03:08 PM
this code isnt working:
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;
}
}
PT Exorcist
Oct 24th, 2002, 03:10 PM
i mean...im getting an error in:
ssBuffer = wcClient.DownloadData(uRL);
and the expcetion isnt being used...why?!?
btw im doin the error just for testing porpose... im testing C# lol
Edneeis
Oct 24th, 2002, 05:09 PM
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")
Lethal
Oct 24th, 2002, 05:26 PM
Here you go, you must also verify that a well-formed uri is passed in:
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);
}
}
PT Exorcist
Oct 24th, 2002, 06:12 PM
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?
Lethal
Oct 24th, 2002, 06:21 PM
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...
catch (WebException e)
{
return "err: " + e.Message;
}
:rolleyes:
PT Exorcist
Oct 24th, 2002, 06:33 PM
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
Lethal
Oct 24th, 2002, 06:37 PM
Sure, remeber that all .NET Languages use the same framework, thus the same objects.
catch (Exception e)
{
return "err: " + e.Message;
}
hellswraith
Oct 24th, 2002, 07:31 PM
You don't even need to specify an exception type if you don't want to:
[code]
try
{
// code here
}
catch
{
// Catches any error
}
Lethal
Oct 24th, 2002, 07:35 PM
good 2 know...:D
PT Exorcist
Oct 25th, 2002, 08:20 AM
yea tks
Lethal
Oct 26th, 2002, 01:13 PM
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.
hellswraith
Oct 26th, 2002, 05:46 PM
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.
Lethal
Oct 26th, 2002, 05:48 PM
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.
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.