Is there "Break on Exception" in IntelliJ? - debugging

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

Related

Vs2019+vs2022 avoid breaking on Xamarin.Essentials.FeatureNotSupportedException

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.

Non helpfull error message Calabash with page objects pattern

I'm currently using Calabash framework to automate functional testing for a native Android and IOS application. During my time studying it, I stumbled upon this example project from Xamarin that uses page objects design pattern which I find to be much better to organize the code in a Selenium fashion.
I have made a few adjustments to the original project, adding a file called page_utils.rb in the support directory of the calabash project structure. This file has this method:
def change_page(next_page)
sleep 2
puts "current page is #{current_page_name} changing to #{next_page}"
#current_page = page(next_page).await(PAGE_TRANSITION_PARAMETERS)
sleep 1
capture_screenshot
#current_page.assert_info_present
end
So in my custom steps implementation, when I want to change the page, I trigger the event that changes the page in the UI and update the reference for Calabash calling this method, in example:
#current_page.click_to_home_page
change_page(HomePage)
PAGE_TRANSITION_PARAMETERS is a hash with parameters such as timeout:
PAGE_TRANSITION_PARAMETERS = {
timeout: 10,
screenshot_on_error: true
}
Just so happens to be that whenever I have a timeout waiting for any element in any screen during a test run, I get a generic error message such as:
Timeout waiting for elements: * id:'btn_ok' (Calabash::Android::WaitHelpers::WaitError)
./features/support/utils/page_utils.rb:14:in `change_page'
./features/step_definitions/login_steps.rb:49:in `/^I enter my valid credentials$/'
features/04_support_and_settings.feature:9:in `And I enter my valid credentials'
btn_ok is the id defined for the trait of the first screen in my application, I don't understand why this keeps popping up even in steps ahead of that screen, masking the real problem.
Can anyone help getting rid of this annoyance? Makes really hard debugging test failures, specially on the test cloud.
welcome to Calabash!
As you might be aware, you'll get a Timeout waiting for elements: exception when you attempt to query/wait for an element which can't be found on the screen. When you call page.await(opts), it is actually calling wait_for_elements_exist([trait], opts), which means in your case that after 10 seconds of waiting, the view with id btn_ok can't be found on the screen.
What is assert_info_present ? Does it call wait_for_element_exists or something similar? More importantly, what method is actually being called in page_utils.rb:14 ?
And does your app actually return to the home screen when you invoke click_to_home_page ?
Unfortunately it's difficult to diagnose the issue without some more info, but I'll throw out a few suggestions:
My first guess without seeing your application or your step definitions is that #current_page.click_to_home_page is taking longer than 10 seconds to actually bring the home page back. If that's the case, simply try increasing the timeout (or remove it altogether, since the default is 30 seconds. See source).
My second guess is that the element with id btn_ok is not actually visible on screen when your app returns to the home screen. If that's the case, you could try changing the trait definition from * id:'btn_ok' to all * id:'btn_ok' (the all operator will include views that aren't actually visible on screen). Again, I have no idea what your app looks like so it's hard to say.
My third guess is it's something related to assert_info_present, but it's hard to say without seeing the step defs.
On an unrelated note, I apologize if our sample code is a bit outdated, but at the time of writing we generally don't encourage the use of #current_page to keep track of a page. Calabash was written in a more or less stateless manner and we generally encourage step definitions to avoid using state wherever possible.
Hope this helps! Best of luck.

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.

Resources