-
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;
}
}
-
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
-
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")
-
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);
}
}
-
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?
-
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;
}
:rolleyes:
-
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
-
Sure, remeber that all .NET Languages use the same framework, thus the same objects.
Code:
catch (Exception e)
{
return "err: " + e.Message;
}
-
You don't even need to specify an exception type if you don't want to:
[code]
try
{
// code here
}
catch
{
// Catches any error
}
-
-
-
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.
-
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.
-
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.