How to reliably catch exceptions?

My app communicates with my server through a number of different endpoint, but they all go through the following method. I'm trying to catch exceptions when communicating to the server so I can convert them to an error response instead. When I run with a debug build this works fine, but when I run with a release build the catch
is never run. This isn't true for all exception handling in my code, but is try for this block. Does anyone know why this happens or have suggestions on how to fix it?
private async Task<HttpResponseMessage> PostAsync(string path, IMessage message)
{
var content = new StringContent(JsonConvert.SerializeObject(message), Encoding.UTF8, "application/json");
content.Headers.Add("x-functions-key", authKey);
try {
Console.WriteLine($"########## PostAsync A {path}");
var result = await client.PostAsync($"{ServiceUriBase}/{path}", content);
Console.WriteLine($"########## PostAsync B {path}");
return result;
} catch(Exception ex) {
Console.WriteLine($"########## {ex}");
return new HttpResponseMessage(System.Net.HttpStatusCode.BadRequest);
}
}
To clarify, with a release build, the PostAsync A
debug message is printed, but no other debug messages after that. So the code is not continuing successfully, and also not catching the exception. I do see a message in the console saying Unhandled managed exception...
, but that's my point; why is this unhandled? I'm trying to catch it.
Hi, Rainer-M. This may be caused by an unhandled exception. To vertify this, try to execute an error code in the
try-catch
block as below:@JarvanZhang , thanks for the reply.
Your try/catch block looks exactly the same as mine. As I said, I already have
catch(Exception ex) { ... }
but it is not catching the exception that is thrown.Sorry for the misleading information. I mean using the above code to check if the 'client.PostAsync' raises an unhandled exception. If the
Console.WriteLine
code of catch block is also not called, the problem is due to thetry-catch
. If not, the issue may be related to 'client.PostAsync' command.The problem is with
client.PostAsync
, other try/catch blocks work fine.From the stack log, the issue is related to the internet. Do you use the same net for test in debug mode and release mode? Try adding the key for the local address as below.
And what is the linking option of the release mode? Please use Link Framework SDKs Only behavior, Link all assemblies option links everything and may modify user code that causes the break.
Yes, I know exactly what the problem is, but that is why I'm trying to catch this error. Basically I don't want my app to crash if the network is down, so I have the try/catch. But in this case the catch is not working so the app crashes.
In this case, the crash is not caused by 'client.PostAsync'. The
try-catch
cannot catch the exetpion.Sorry, I don't understand. The crash is definitely caused by the
client.PostAsync
. In my original post you can see I includeConsole.WriteLine
statements before and after theclient.PostAsync
. The WriteLine before it is executed, but neither the WriteLine after it, nor the WriteLine in thecatch
is executed. This clearly means theclient.PostAsync
is run, but fails and causes the crash. My question is, why isn't the exception caught?I didn't see the detail Xamarin Exception Stack, but it thrown by the
NSUrlSessionHandler
on iOS. There is a similar issue on github, you could try to add the exception handling to your custom NsUrlSessionHandler class, and then register that class with DependencyService .In addition, you said it only exists on release mode, may I ask if you archive an ipa and install it on your device? Would mind sharing the detail exception information?
Sign in to comment
Activity