Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a try catch around some code that gets a response from a web request. The following is the code

C#
var token = "";
try
  {
    var response = req.GetResponse();
    using (var sReader = new streamReader(response.GetResponseStream()))
    {  
       token = sReader.ReadToEnd();
     }
catch (webException we)
{
   token = "ERROR: " + we;
}
catch (Exception e)
{
   Token = "ERROR: " + e;
}



This line -
var response = req.GetResponse();
is the one that fails. The fact it fails is a different issue that I'm working on with a different group.

The problem is the catch statement only catches the error on 2 of the 3 test machines. The third one it doesn't catch and I get a unhandled exception error.

What I have tried:

Somewhere I read that the issue could be a mismatch .NET version, so I've made sure the app targets .NET 4.6 and all 3 machines have 4.6 or greater installed.

Machine 1 - Windows 10, .NET 4.6.2 - catch block works
Machine 2 - Server 2008, .NET 4.6 - catch block works
Machine 3 - Server 2008, .NET 4.6 - catch block does not work.

What else can I look at?
Posted
Updated 30-Jan-18 16:11pm
Comments
j snooze 30-Jan-18 17:08pm    
Can you post the stack trace?
Member 10935450 30-Jan-18 17:13pm    
Is there something I should be looking for in there? I ask, because copying and pasting is difficult from the computer where the code is, i'm hand typing the code block in.
j snooze 30-Jan-18 17:17pm    
I just wanted to verify the errors were similar between the two. If I take your code at face value its interesting that you have token defined and used it in the web exception but use Token with a capital "T" in the regular exception catch. Thats using an undefined variable in general exception, but if you hand typed it, might just be a typo. Thought maybe the 2 different stack traces (usually the top error) may say something different that could lead you to what might be missing or different. Not having the error leaves me with nothing to go on or speculate.
Member 10935450 30-Jan-18 17:32pm    
It's a typo, sorry can't figure out how to edit the question. On the two working machines the catch block works, I then take the error presented in Token and log it, then handle it separate giving the user a clean page. In the one that fails, I'm getting the authentication failed error message that I'm expecting on the web response and trying to trap for. If it makes a difference, this is a website and the error when it occurs shows on the web page. It's showing upon the page as an unhandled exception error.
Valery Possoz 30-Jan-18 17:32pm    
Maybe a non-CLSCompliant exception is thrown and Catch(Exception e) will not catch it.
You actually need a catch {balh blah;}, notice the lack of brackets...

See here: https://msdn.microsoft.com/en-us/library/bb264489.aspx

1 solution

It sounds like the exception thrown is outside the area of code that you are looking at. You need to catch the error a different way.

I have app wide exception handlers that I use. You can find examples of WinForm and WPF implementation in this article: Silent ClickOnce Installer for Winform & WPF in C# & VB[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900