By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,971 Members | 900 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,971 IT Pros & Developers. It's quick & easy.

Try/Catch

P: n/a
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;
}
}
}

Oct 23 '07 #1
Share this Question
Share on Google+
9 Replies


P: n/a
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]
- mv*@spam.guard.caspershouse.com

"SMH" <si**********@googlemail.comwrote in message
news:11**********************@t8g2000prg.googlegro ups.com...
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;
}
}
}

Oct 23 '07 #2

P: n/a

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/archi...16/396787.aspx

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

"SMH" <si**********@googlemail.comwrote in message
news:11**********************@t8g2000prg.googlegro ups.com...
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;
}
}
}

Oct 23 '07 #3

P: n/a
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.

On Oct 23, 8:42 pm, "Nicholas Paldino [.NET/C# MVP]"
<m...@spam.guard.caspershouse.comwrote:
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]
- m...@spam.guard.caspershouse.com

"SMH" <simonmhar...@googlemail.comwrote in message

news:11**********************@t8g2000prg.googlegro ups.com...
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;
}
}
}

Oct 23 '07 #4

P: n/a
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]
- mv*@spam.guard.caspershouse.com

"SMH" <si**********@googlemail.comwrote in message
news:11**********************@e9g2000prf.googlegro ups.com...
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.

On Oct 23, 8:42 pm, "Nicholas Paldino [.NET/C# MVP]"
<m...@spam.guard.caspershouse.comwrote:
>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]
- m...@spam.guard.caspershouse.com

"SMH" <simonmhar...@googlemail.comwrote in message

news:11**********************@t8g2000prg.googlegr oups.com...
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;
}
}
}


Oct 23 '07 #5

P: n/a

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" <si**********@googlemail.comwrote in message
news:11**********************@e9g2000prf.googlegro ups.com...
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.

On Oct 23, 8:42 pm, "Nicholas Paldino [.NET/C# MVP]"
<m...@spam.guard.caspershouse.comwrote:
>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]
- m...@spam.guard.caspershouse.com

"SMH" <simonmhar...@googlemail.comwrote in message

news:11**********************@t8g2000prg.googlegr oups.com...
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;
}
}
}


Oct 23 '07 #6

P: n/a
On Tue, 23 Oct 2007 12:30:43 -0700, SMH <si**********@googlemail.com>
wrote:
>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

--
http://bytes.thinkersroom.com
Oct 23 '07 #7

P: n/a
SMH <si**********@googlemail.comwrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Oct 23 '07 #8

P: n/a
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" <si**********@googlemail.comwrote in message
news:11**********************@e9g2000prf.googlegro ups.com...
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.

On Oct 23, 8:42 pm, "Nicholas Paldino [.NET/C# MVP]"
<m...@spam.guard.caspershouse.comwrote:
>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]
- m...@spam.guard.caspershouse.com

"SMH" <simonmhar...@googlemail.comwrote in message

news:11**********************@t8g2000prg.googlegr oups.com...
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;
}
}
}

Oct 24 '07 #9

P: n/a
SMH
Guys, thank you all very much for your posts.

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

Simon.

Oct 24 '07 #10

This discussion thread is closed

Replies have been disabled for this discussion.