Try/Catch

S

SMH

Hi,

I am making the transition from VB to C#

I am struggling to get my try/catch to work. For some reason, the
exception is not caught. My code is below, any help will be much
appreciated.

Thank you.

Simon.

protected void btnDownloadFile_Click(object sender, EventArgs e)
{
System.Net.WebClient MyClient = new System.Net.WebClient();
for (int i = 0; i < 10; i++) {
try {
MyClient.DownloadFile("http://www.google.co.uk/images/
firefox/fox1.gif", @"C:\afolder\firefox" + i + ".gif");
}
catch (UnauthorizedAccessException aa) {
this.lblException.Text += aa;
}
}
}
 
S

sloan

try {
MyClient.DownloadFile("http://www.google.co.uk/images/
firefox/fox1.gif", @"C:\afolder\firefox" + i + ".gif");
}
catch (UnauthorizedAccessException aa) {
this.lblException.Text += aa.Message;
}


aa is a UnauthorizedAccessException
..Text is a string.


If you did not have
Option Strict On
Option Explicit On

in your Vb.net code, you'll run into these alot.

also check to see if DownloadFile returns anything....


Bookmark this page:
http://blogs.msdn.com/kcwalina/archive/2005/03/16/396787.aspx

It has great info about how/when and what not to do.
 
N

Nicholas Paldino [.NET/C# MVP]

SMH,

If you change the type of the exception in the catch from
UnauthorizedAccessException to Exception, does it catch anything? My guess
is that it's a different exception type that is being thrown.
 
S

SMH

Hi Nicholas,

I changed it to Exception, and yes - The exception is now caught.
However, I am confused as the exception text written to my label was:

System.Net.WebException: An exception occurred during a WebClient
request. ---> System.UnauthorizedAccessException: Access to the path
'C:\afolder\firefox0.gif' is denied.

Surely I had this setup originally to catch
System.UnauthorizedAccessException? Or should I have been catching the
system.net.WebException? When the error was thrown outside of the try/
catch, the Framework told me I had a
System.UnauthorizedAccessException.

To be honest, I am a little stuck here!

Thank you for your help!

Simon.

SMH,

If you change the type of the exception in the catch from
UnauthorizedAccessException to Exception, does it catch anything? My guess
is that it's a different exception type that is being thrown.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


I am making the transition from VB to C#
I am struggling to get my try/catch to work. For some reason, the
exception is not caught. My code is below, any help will be much
appreciated.
Thank you.

protected void btnDownloadFile_Click(object sender, EventArgs e)
{
System.Net.WebClient MyClient = new System.Net.WebClient();
for (int i = 0; i < 10; i++) {
try {
MyClient.DownloadFile("http://www.google.co.uk/images/
firefox/fox1.gif", @"C:\afolder\firefox" + i + ".gif");
}
catch (UnauthorizedAccessException aa) {
this.lblException.Text += aa;
}
}
}
 
S

sloan

But Nicholas has a better guess I think.

...

Since the poster is coming from VB.Net, maybe

Me.lblException.Text += aa

actually worked??? Who knows?




sloan said:
The original post was this:
catch (UnauthorizedAccessException aa) {
this.lblException.Text += aa;
}


Thus my post.
 
S

SMH

Sloan,

Thank you - Have bookmarked that page.

Agree with what you say - However, when I try lbl.text = aa.tostring,
I get the following compilation error:
- Compiler Error Message: CS0019: Operator '+' cannot be applied to
operands of type 'method group' and 'string'

It seems to work Ok without .tostring, and the exception is now
caught, although you will see from my post above that I am not sure
why the original code did not work as from what I can gather I was
catching the expected exception.

Any further info much appreciated!

Simon.
 
S

sloan

Check the
InnerException

and see if that matches it up correctly.

as in

try
{
//do stuff
}
catch ( System.Net.WebException wex )
{
if(null!= wex.InnerException)
{
string x = wex.InnerException.Message;
}
}
catch ( Exception ex )
{
if(null!= ex.InnerException)
{
string x = ex.InnerException.Message;
}
}




SMH said:
Hi Nicholas,

I changed it to Exception, and yes - The exception is now caught.
However, I am confused as the exception text written to my label was:

System.Net.WebException: An exception occurred during a WebClient
request. ---> System.UnauthorizedAccessException: Access to the path
'C:\afolder\firefox0.gif' is denied.

Surely I had this setup originally to catch
System.UnauthorizedAccessException? Or should I have been catching the
system.net.WebException? When the error was thrown outside of the try/
catch, the Framework told me I had a
System.UnauthorizedAccessException.

To be honest, I am a little stuck here!

Thank you for your help!

Simon.

SMH,

If you change the type of the exception in the catch from
UnauthorizedAccessException to Exception, does it catch anything? My
guess
is that it's a different exception type that is being thrown.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


I am making the transition from VB to C#
I am struggling to get my try/catch to work. For some reason, the
exception is not caught. My code is below, any help will be much
appreciated.
Thank you.

protected void btnDownloadFile_Click(object sender, EventArgs e)
{
System.Net.WebClient MyClient = new System.Net.WebClient();
for (int i = 0; i < 10; i++) {
try {
MyClient.DownloadFile("http://www.google.co.uk/images/
firefox/fox1.gif", @"C:\afolder\firefox" + i + ".gif");
}
catch (UnauthorizedAccessException aa) {
this.lblException.Text += aa;
}
}
}
 
N

Nicholas Paldino [.NET/C# MVP]

SMH,

It looks like the UnauthorizedAccessException is the inner exception,
not the actual exception that is thrown. Perhaps the other code you had was
doing something to the exception?

Regardless, you should be catching the WebException and then examine
that for more detailed information.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

SMH said:
Hi Nicholas,

I changed it to Exception, and yes - The exception is now caught.
However, I am confused as the exception text written to my label was:

System.Net.WebException: An exception occurred during a WebClient
request. ---> System.UnauthorizedAccessException: Access to the path
'C:\afolder\firefox0.gif' is denied.

Surely I had this setup originally to catch
System.UnauthorizedAccessException? Or should I have been catching the
system.net.WebException? When the error was thrown outside of the try/
catch, the Framework told me I had a
System.UnauthorizedAccessException.

To be honest, I am a little stuck here!

Thank you for your help!

Simon.

SMH,

If you change the type of the exception in the catch from
UnauthorizedAccessException to Exception, does it catch anything? My
guess
is that it's a different exception type that is being thrown.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


I am making the transition from VB to C#
I am struggling to get my try/catch to work. For some reason, the
exception is not caught. My code is below, any help will be much
appreciated.
Thank you.

protected void btnDownloadFile_Click(object sender, EventArgs e)
{
System.Net.WebClient MyClient = new System.Net.WebClient();
for (int i = 0; i < 10; i++) {
try {
MyClient.DownloadFile("http://www.google.co.uk/images/
firefox/fox1.gif", @"C:\afolder\firefox" + i + ".gif");
}
catch (UnauthorizedAccessException aa) {
this.lblException.Text += aa;
}
}
}
 
R

Rad [Visual C# MVP]

Hi,

I am making the transition from VB to C#

I am struggling to get my try/catch to work. For some reason, the
exception is not caught. My code is below, any help will be much
appreciated.

Thank you.

Simon.

protected void btnDownloadFile_Click(object sender, EventArgs e)
{
System.Net.WebClient MyClient = new System.Net.WebClient();
for (int i = 0; i < 10; i++) {
try {
MyClient.DownloadFile("http://www.google.co.uk/images/
firefox/fox1.gif", @"C:\afolder\firefox" + i + ".gif");
}
catch (UnauthorizedAccessException aa) {
this.lblException.Text += aa;
}
}
}

It could be some other type of exception altogether - perhaps your
proxy server denied you access or the file could not be retrieved from
the web.

Change the UnauthorizedAccessException to something more generic like
Exception
 
J

Jon Skeet [C# MVP]

sloan said:
The original post was this:


Thus my post.

Ah. It's really not a good idea to change the quoted text, otherwise it
looks like it was like that from the start :)
 
J

Jon Skeet [C# MVP]

SMH said:
Thank you - Have bookmarked that page.

Agree with what you say - However, when I try lbl.text = aa.tostring,
I get the following compilation error:
- Compiler Error Message: CS0019: Operator '+' cannot be applied to
operands of type 'method group' and 'string'

Yes, it would have to be "ToString()". You need to put the parentheses
on method calls in C#.
It seems to work Ok without .tostring

Actually, that does make sense. It's like this:

string x = "";
x += 5;

works fine, because it's doing:

x = x+5;

which then implicitly uses string concatenation.
and the exception is now
caught, although you will see from my post above that I am not sure
why the original code did not work as from what I can gather I was
catching the expected exception.

Explained elsewhere - it's the inner exception, by the looks of it.
 
S

sloan

Find my other post about the InnerException and sample code.


You probably want to use the .Message instead of the the .ToString() as a
general habit.

I've noticed (including myself when it was happening), that a VB.Netter
transferring to C# has a tendency to overuse the .ToString().

Here is another place

int age = 20;
int height = 72;//inches that is
string x = string.Format ( "This is the first thing {0}, and this is the
second thing {1}, age , height ) ;

But check the other post, and sample code.
 
J

Jon Skeet [C# MVP]

sloan said:
Find my other post about the InnerException and sample code.

You probably want to use the .Message instead of the the .ToString() as a
general habit.

That depends - if you want the stack trace as well, ToString() is very
handy :)

<snip>
 
A

Andrew Faust

Perhaps you need to catch both exceptions. Many classes can throw multiple
exception types. It could be that in your earlier tests you were getting
the Unauthorized exception, but now you are getting the other exception
type due to different reasons.

--
Andrew Faust
andrew[at]andrewfaust.com
http://www.andrewfaust.com


SMH said:
Hi Nicholas,

I changed it to Exception, and yes - The exception is now caught.
However, I am confused as the exception text written to my label was:

System.Net.WebException: An exception occurred during a WebClient
request. ---> System.UnauthorizedAccessException: Access to the path
'C:\afolder\firefox0.gif' is denied.

Surely I had this setup originally to catch
System.UnauthorizedAccessException? Or should I have been catching the
system.net.WebException? When the error was thrown outside of the try/
catch, the Framework told me I had a
System.UnauthorizedAccessException.

To be honest, I am a little stuck here!

Thank you for your help!

Simon.

SMH,

If you change the type of the exception in the catch from
UnauthorizedAccessException to Exception, does it catch anything? My
guess
is that it's a different exception type that is being thrown.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


I am making the transition from VB to C#
I am struggling to get my try/catch to work. For some reason, the
exception is not caught. My code is below, any help will be much
appreciated.
Thank you.

protected void btnDownloadFile_Click(object sender, EventArgs e)
{
System.Net.WebClient MyClient = new System.Net.WebClient();
for (int i = 0; i < 10; i++) {
try {
MyClient.DownloadFile("http://www.google.co.uk/images/
firefox/fox1.gif", @"C:\afolder\firefox" + i + ".gif");
}
catch (UnauthorizedAccessException aa) {
this.lblException.Text += aa;
}
}
}
 
K

KWienhold

Find my other post about the InnerException and sample code.

You probably want to use the .Message instead of the the .ToString() as a
general habit.

I've noticed (including myself when it was happening), that a VB.Netter
transferring to C# has a tendency to overuse the .ToString().

Here is another place

int age = 20;
int height = 72;//inches that is
string x = string.Format ( "This is the first thing {0}, and this is the
second thing {1}, age , height ) ;

But check the other post, and sample code.










- Zitierten Text anzeigen -

As Jon already pointed out, Message returns only the actual text of
the error message, while ToString() returns the stack-trace as well.
So you would normally use Message when you want to display the
exception to the user, but ToString for logging purposes.
Displaying the stack trace to your users is probably confusing and can
actually be embarrassing if your colleagues aren't adhering to naming
conventions, I once had a customer call and tell me that an Exception
was thrown in "SpeedyGonzales.GoGoGo()"...
Another interessting thing is that normally the compiler would tell
you that you are missing the parentheses after ToString, but since "+=
Methodname" is accepted shorthand for generating delegate instances in
C# 2 the error message becomes less clear.

Kevin Wienhold
 
S

SMH

Guys, thank you all very much for your posts.

It was indeed throwing System.Net.WebException which I am now catching
successfully.

Simon.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top