Vs2019+vs2022 avoid breaking on Xamarin.Essentials.FeatureNotSupportedException - xamarin

When using sensors such as barometer,compas etc. in Xamarin.Forms you cannot check if the sensor is available but have to try starting it and then catch the exception, if it is not there, such as here.
try
{
Barometer.Start(SensorSpeed.Game);
}
catch
{
//failed
}
The issue is that this happens every single time, I debug on a device without these sensors, and though I catch the exception, I break into it all the time.
The exception is
Xamarin.Essentials.FeatureNotSupportedException: 'Specified method is not supported.'
but when I, in exception settings, uncheck that one from "break when thrown", it still breaks on the exception.
This derives from
System.NotSupportedException
which i then also uncheck, and still it breaks on it.
The next levels up are
System.SystemException
System.Exception
which I can uncheck to stop breaking when thrown, but I would rather do that, since then everything is effectively not breaking if caught, which I would still like.

Related

Php7 practices regarding Throwable errors

Long story short is that we've recently switched from php 5.6 to php 7. Although the application (a Magento 1.9 based app) seems to work accordingly after the switch, we have some shell scripts which somehow/sometimes die all of the sudden without knowing why:
Mage::log("doing something", null, 'custom.log', true); <- logged, see it
doSomething(); <- perfroms a service call, which looks like it's done properly
Mage::log("something done", null, 'custom.log', true); <- not logged
The problem in the above is that we have absolutely no error, warning, etc. so we cannot determine what happens. Since the changing of error handling in Php7, I assume that an error occurs (which previously was being logged in /var/log/...) and now is thrown as an Error exception, which based on Php7 specifications is not caught by a classic catch(Exception $e){} block. So my main questions regarding this whole situation is:
When switching from Php5.X to Php7, wouldn't it be a good practice to change the catch(Exception $e) with catch(Throwable $e) as Throwable covers both Excptions and new Errors? Otherwise, if I'm correct, one should add multiple "catch (Error $e)" blocks inside the code where this could occur in order to catch these error now.

Is it possible to add additional information for crashes handled by Xamarin.Insights analytics framework

I have an xamarin.android with xamarin.insights intergrated.
Right now every time I handle error manually (try/catch) I'm adding information about environment (staging/production):
try
{
ExceptionThrowingFunction();
}
catch (Exception exception)
{
exception.Data["Environment"] = "staging";
throw;
}
But this information is missing in case if error handled by xamarin.insights itself (in case of crash).
It is possible to add additional exception data in case of crash?
docs reference I used
From reading the docs page reference that you mentioned, I still get the impression that you have to call the .Report method as well as in:-
Insights.Report(exception, new Dictionary <string, string> {
{"Some additional info", "foobar"}
});
What I believe they are saying in this example:-
try {
ExceptionThrowingFunction();
}
catch (Exception exception) {
exception.Data["AccountType"] = "standard";
throw;
}
Is that you have the ability when any Exception is encountered, to package additional information that you can later send to the Insights server, as the Data property of the Exception is just a Key/Value Dictionary.
So if you had an Exception several layers deep, you can choose to re-throw the Exception with additional information contained within it that you will later send to the Insights server.
At a higher level, you can then take the Exception that was thrown deeper down the call-hierarchy and then call the Insights.Report, with:-
Insights.Report(
{the rethrown exception in your higher up try..catch block},
{rethrown exception}.Data
);
that will then send all the additional Key/Value information previously captured.
From seeing your last part of your question though it looks like you are interested in Insights handling and sending this additional .Data automatically should there be an unhandled exception.
If it is not currently being sent, then perhaps suggest to them that this can be sent also? As it sounds a feasible request for this to automatically be sent as well incase of an unhandled exception.
Update 1:-
Yes - I understand about the unhandled exception scenario now that you are referring to.
I have not dealt with this component directly, so there may be hooks / event handlers or something already defined where you can tap into this, and execute some custom code just prior to this being sent.
If this is not available, then perhaps suggest this to them to include as its a Beta product?
Alternatively, you could still achieve this yourself by capturing the unhandled exceptions just prior to them falling. You'd have to code this however on each platform.
For instance on Windows Phone in the App class there is Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e) to which you could then supplement the Exception thrown with this extra .Data.
For Android you could take a look at this post that describes how to catch uncaughtException that will help you in capturing the unhandled exceptions.
Whether just supplementing the Exception in these handlers above is enough all depends on how they've written their hook into this, as to how well it behaves and whether it is executed first, prior to their implementation.
You will have to try and see if it does. If it doesn't behave well, allowing you to supplement extra data prior to the automatic call to Insights, you have another fallback solution, to just do the .Report call manually within these unhandled exception handlers yourself to make this work and supplement the extra .Data to achieve your aim.

How to treat 0x40010006 exception in vectored exception handler?

I register my own vectored exception handler, to catch and trace various exceptions in my application.
Sometimes I get an exception having 0x40010006 code, which is thrown by OutputDebugString function. Certainly, I'd like just to ignore it. What would be the appropriate return value in this case: EXCEPTION_CONTINUE_EXECUTION or EXCEPTION_CONTINUE_SEARCH?
You'll find exception codes listed in the ntstatus.h SDK header file. This one is DBG_PRINTEXCEPTION_C, some likelihood that you typed Ctrl+C to trigger it.
Exception codes with values less than 0x80000000 are just informal and never an indicator of real trouble. In general, you should never mess with exceptions that you don't recognize and don't want to explicitly handle. Let Windows continue searching for a handler by returning EXCEPTION_CONTINUE_SEARCH, the debugger will probably catch it in this case.

Exception handling strategy

Imagine you have some code that could potentially throw an exception. For example, you try
to send an e-mail message to a mail server, or write a file to disk while you’re not sure if you have the right permissions to do so. What kind of exception handling strategy would you use to avoid the exception from being displayed in the browser? What code would you need?
All languages that can throw exceptions have some manner by which to catch them.
They often look something like this:
try
{
some_risky_thing();
}
catch(Exception e)
{
handle_the_exception();
}
By catching the exception you stop it's propagation up the call stack (where it will eventually find the user).
In order to stop all exceptions getting to the user put one of these at the very top level you have available. Then you can catch any stray exceptions you've missed and do something more appropriate than throw them at the user (like log them somewhere discretely).
It depends.
For those cases, I would probably wrap the code that can throw the exception in a try/catch block. Different languages call this construct something different - sometimes it's try/catch/finally, others it's try/except.
However, it's easy to abuse exceptions and exception handling. A few things that you need to avoid are using exception handling for flow control, handling exceptions too soon (keep passing them up the call stack until they can be appropriately handled), and treating non-exceptional conditions as exceptional.

Is there "Break on Exception" in IntelliJ?

Is there feature that will automatically break debugging on first exception occurrence?
So we
start application
do something that throw exception
got IntelliJ popped up highlighted line where exception occurred.
Run | View Breakpoints | Exception Breakpoints
A fast way to pop up the dialog is to press Ctrl + SHIFT + F8 (On Mac: Cmd + SHIFT + F8), then click over to the exception breakpoints tab. If that was the last tab you were viewing, it'll still be selected, making it easy to flick breaking on exceptions on and off.
This will cause IntelliJ to break at the point in the code (or library code) where the exception was raised. Specifically, you get a 'first chance' at exception handling, before the stack is walked looking for catch/finally blocks to execute.
TIP: Java tends to throw a lot of exceptions internally when loading classes, so this breaking on all exceptions can become quite tedious. The good news is that you can exclude certain types of exception using the condition field.
For example:
!(this instanceof java.lang.ClassNotFoundException)
You can chain multiple such conditions together with &&.
In IntelliJ IDEA 14 go to:
Run -> View Breakpoints -> Check "Java Exceptions Breakpoints" -> Uncheck "Caught Exceptions"
If you do not uncheck Caught Exceptions the execution will be stopped every time the Java Framework throws an internal exception.
In newer versions of intellij, it is under Run > View Breakpoints.
Then you can check Java Exception Breakpoints -> Any Exception.
A good way to debug the exceptions is to use your main app package and the wild card .*. This way you skip all the other libraries exceptions, since most of the times you are looking for exceptions throwed by your app and not by any other library (which can be a lot of exceptions).
As an example showed in the image, I use com.gs.mercury.* to break every time the app throws an exception. If you use exceptions for what they are for (to handle exceptional cases and not to handle the flow of normal situations) you will only stop when you reach the desired exception almost all the time.
You would use Catch class filters to specify the classes of the exceptions you are expecting to catch, and Class filters to specify in which classes you expecing to catch the exception.
Note: Added the clarification between Catch class filters and Class filters made by #Roman in the comments.
PS. answer added just to point out the pretty useful Catch class filters and Class filters.
Yes, there is. You need to define an exception breakpoint (it can be "Any exception") in the breakpoints dialog in IntelliJ IDEA.
The exceptions can be filtered by condition or class if desired, or by whether you are interested in caught or uncaught exceptions.
If you click on the little "+" sign in the upper left corner, you can add a new breakpoint. If you select Exception Breakpoint, you get a little dialog where you can enter the exception class to break on (in case you don't want to break on all exceptions).

Resources