473,383 Members | 2,005 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

Try/Catch

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
9 2656
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

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
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
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

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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Gary.Hu | last post by:
I was trying to catch the Arithmetic exception, unsuccessfully. try{ int a = 0, b = 9; b = b / a; }catch(...){ cout << "arithmetic exception was catched!" << endl; } After ran the program,...
11
by: kaeli | last post by:
Hey all, I'd like to start using the try/catch construct in some scripts. Older browsers don't support this. What's the best way to test for support for this construct so it doesn't kill...
4
by: Abhishek Srivastava | last post by:
Hello All, I have seen code snippets like try { ..... } catch {
11
by: Pohihihi | last post by:
I was wondering what is the ill effect of using try catch in the code, both nested and simple big one. e.g. try { \\ whole app code goes here } catch (Exception ee) {}
13
by: Benny | last post by:
Hi, I have something like this: try { // some code } catch // note - i am catching everything now {
23
by: VB Programmer | last post by:
Variable scope doesn't make sense to me when it comes to Try Catch Finally. Example: In order to close/dispose a db connection you have to dim the connection outside of the Try Catch Finally...
32
by: cj | last post by:
Another wish of mine. I wish there was a way in the Try Catch structure to say if there wasn't an error to do something. Like an else statement. Try Catch Else Finally. Also because I...
23
by: pigeonrandle | last post by:
Hi, Does this bit of code represent complete overkill?! try { //create a treenode TreeNode tn = new TreeNode(); //add it to a treeview tv.Nodes.Add(tn);
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.