Breaking on your own exceptions in IntelliJ - java

See this question on how to break on any exceptions.
I'm getting a million exceptions on startup. I've tried ignoring ClassNotFoundException, but it's no good, the IDE still seems to break on these (and other) exceptions.
So .. what's a decent configuration for this to catch only real exceptions caught from user code? (Ignore also any exception in jUnit, if applicable)

As mentioned by the other posters, Class Filters are the way to do this.
Specifically, you could add the package names for what you consider "your code" as a class filter. The syntax is a little opaque, but using a simple wildcard filter like:
com.whatever.package.*
sounds like it it will work for you. IntelliJ will break on any exception in any class in any package under here. You can of course define multiple wildcard filters if your code is in more than one place.

I was just messing with this earlier. Two ways to go:
1) Configure it to explicitly catch your exception, not "any exception" - if it's something like RuntimeException, this may not be filter-y enough.
2) Use the class results filter - this for whatever reason did NOT work for me. Ever.

I checked IJ and I see tons of options. Maybe the class filters is your option?

I'm adding this answer because I had a hard time finding the Class Filters sub-dialog mentioned in serg10's accepted answer. To see that sub-dialog do the following:
1) Open the View Breakpoints dialog using the following keyboard shortcut (Ctrl + Shift + F8), assuming you have not changed the binding for that shortcut.
2) Check the entry in the left hand column titled" "Java Exception Breakpoints". That should automatically check the child item titled "Any Exception" belonging to that top level element.
3) Click directly on the child item titled "Any exception". You should now see the Class filters drop-down box on the right hand side.
4) Check the check-box next "Class Filters". The View Breakpoints dialog should now look like this:
5) Click on the ellipsis next to the Class Filters drop-down box. You should now see the Class Filters dialog.

See these instructions to create a specific exception breakpoint. In short:
On the main menu, choose Run | View Breakpoints, or press ⇧⌘F8.
In the Breakpoints dialog box that opens, click add.
Select Exception Breakpoints from the drop-down list.
(source: jetbrains.com)
In the Select Exception Class dialog box, specify the desired exception class from the library, or from the project, and click OK.

Related

The code behind filesystem.normalize

I'm new to Java and OOP. I have a strange disorder that makes me extremely curious how things work. So I copied this code that use normalize() and I'm trying to get the source code of it but all I found is
public abstract String normalize(String path);
In:
FileSystem.java file
Of course, know what it does. But I'm curious how and why i cant find the code behind it.
Thank you
Michal
If you are using an IDE (like Eclipse), you can use it to find the concrete implementations of that method.
In Eclipse:
Ctrl + Shift + T and type java.io.FileSystem; click "OK"
Hover over the normalize method name. A menu will appear like below.
Click "Open Implementation"
If there is more than one implementation available, you will see a list. Click on the one you want to open.
If there is only one, then that single implementation will be opened automatically.

Is there any way to set breakpoints on all methods of a class?

Is there any way to set breakpoints on all methods of a given class?
I have a huge (2300 lines) legacy class and I need to set breakpoints on all method calls to understand how this mess works.
You can follow the steps below:
Run -> View breakpoints -> Add -> Java Method Breakpoints
Class pattern -> full reference of your class (e.g., mypackage.MyClass)
Method Name -> * (i.e., asterisk wild card)
I have discovered workaround :
1. I have set "Toggle Brakepoint" hotkey to Alt+Numpad 0.
2. After that you can click on first method
3. Use "Toggle Brakepoint"
4. Alt+Down - goto Next Method. ( Alt+Up - goto Previous Method. )
5. Repeat 3 step.
This is similar to Sergey Senkov's answer, but without hotkeys.
In the structure view, click on the first method. Repeat the following for each method:
Context Menu Key
M to toggle the method breakpoint.
Down
There is a plugin for idea:
Simple Toggle All Method Breakpoint.
It allows you to breakpoint all methods / clear all method's breakpoints in one click from context menu on class in Project view.
As Andrey Lavrukhin suggested, there is Simple Toggle All Method Breakpoint, install it through Settings -> Plugins. Works perfectly.
The only way you'll be able to do what you want is to set method breakpoints on each and every method with the class in question. You need to click on the left hand gutter next to the method - a little red circle with 4 dots will appear and you may get a warning saying method level breakpoints can impact performance. You can then further configure the breakpoint (by rightclicking on it, or select shift+F8) and set it so that it breaks on entry, exit or both
I'm afraid theres no way to do this in a single step/setting.

How to view class package type and parameters in IntelliJ?

When hovering your mouse over a Class or variable, how come I don't see any information on the type of the class?
Is this not a feature in IntelliJ?
For example, in vs.net, if I mouse over any variable or class it will popup and tell me what namespace that belongs to etc.
And when I am using a method of a class, it also tells me the different overloads for the method (like types for each parameter, and a list of all the overloads).
This must be a feature in IntelliJ, I just don't know how to get it.
Can someone please clear this up?
Also, how can I tidy up the formatting of a page?
It doesn't work on hover yet, please star/vote the issue.
You need to use keyboard shortcuts for quick documentation pop-up or parameter info pop-up (Ctrl+Q and Ctrl+P with default Windows keymap, F1 and Cmd+P with default OS X 10.5+ keymap).
Code | Reformat Code... is the answer to your second question.
Formatting: Code -> Reformat (shortcut: Ctrl-Alt-L)
Hover doesn't produce the kind of information I'd like either. You can get info with Ctrl-Q ("Quick Help") in Windows or F1 in OS X, which will show where it's from and what it is, with most stuff in the popup window linked up in a reasonable way.
That popup view can also be pinned and/or docked; I often have it docked on the bottom.

How to get back auto-completion after misspelling a method name in Eclipse?

When I am coding Java in Eclipse I like the auto-completion feature. With that I mean the popup with method-names that comes when you start typing in a method name for an object. Or maybe it's called something different, i.e. method-suggestions?
But the popup is hidden if I misspells a method name, and it doesn't come back if I delete the misspelled part of the method name. Is there any way to get back the popup after a misspelling without starting to type in the hole method name again?
Press Ctrl+ (Blank). For a complete list of keyboard shortcuts have a look in the eclipse "Preferences" and there "General/Keys".
You should also check out Preferences->Java->Editor->Content Assist. You'll be able to select how it acts; things like if you use it in the middle of a word should it insert or overwrite, should it show deprecated methods, the delay before it automatically appears, and it can even (try to) guess your method parameters based on the variables in the current scope.
I think you're after the Ctrl-Space keyboard shortcut.
(In Eclipse this is called Content Assist. In Visual Studio it's called IntelliSense.)

Debugging in Eclipse (Java); can't hover over a variable when breaking to view the value

I'm using Eclipse to code Java (for Android) and I'm trying to debug the code as I normally do (i normally do C# though).
From what I can tell, debugging in Eclipse is really bad. I don't know if I'm doing something wrong, but it seems to be just awful.
This is the code that is being run, I get some sort of exception and I want to see what the Exception is, by breaking in the "catch"-clause and viewing the variable "e":
try
{
ConnectivityManager connectivityManager = (ConnectivityManager) this.getSystemService( Context.CONNECTIVITY_SERVICE );
NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();
NetworkInfo mobNetInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
NetworkInfo.State state = mobNetInfo.getState();
}
catch(Exception e)
{
Log.v("MyAp", e.toString()); // I break on this line, and want to see what e is
}
The problem is that I cannot see "e" by holding my mouse over it (as I can in my beloved Visual Studio).
Now, I know that I can bring up the tab "Variables" which lists all variables accessible from when I'm breaking, but that's not optimal.
Just so I know - Eclipse has no way of "hovering" over the variable you are interested in, as in Visual Studio?
Edit
Thanks for the answers. However, still have the same problem =(
Go to Preferences-> Java-> Editor-> Hovers and tick the box 'Variable Values'. If you want Hover to happen automatically then leave the Key Modifier section blank.
There seem to be bugs in Eclipse related to this:
https://bugs.eclipse.org/bugs/show_bug.cgi?id=317045
https://bugs.eclipse.org/bugs/show_bug.cgi?id=330061
https://bugs.eclipse.org/bugs/show_bug.cgi?id=286237
So, yes it is supposed to work, but it doesn't always. I'm having this issues on some classes. It seems to be consistent which classes work and which don't, probably related to what jar the class is coming from etc, but I have no real clue as to what causes it. Would love to see some reproduction scenario.
You can also highlight the variable, right click, and choose to watch it. Then it shows up on the watch tab. You can also set automatic break points that trigger on the exception being thrown rather than at a particular line of code. There is not 'hover' option to view the variable as in Visual Studio in recent versions of Eclipse.
This might seem a bit strange, but do you know that the code window has focus? I have observed that sometimes the appearance of the tooltip depends on whether a view has focus or not (which I guess is an SWT bug).
I have also found that in the "Debug" view (the tab with the little green bug as an icon) where it shows all the threads, Eclipse sometimes doesn't show the variable's values if the currently breakpointed thread isn't selected.
If anyone else runs across this problem, then you might have hit a breakpoint, but the currently running thread isn't the one highlighted in the "Debug" window.
As far as I can tell, you have to be in Debug view in order to hover over objects and see their properties. At least that's how my copy of Eclipse 3.4 works.
Of course, you also need to be in Debug view to have the Step Into, Step Over, Continues, etc... buttons...
Go to Preferences -> Java -> Editor -> Hovers, and click Restore Defaults.
Found it, Goto: Preferences > Java > Editor > Hovers - Javadoc (click on it and remove the ctrl+shift keys from there), then it should work normally.
I think that the correct answer is the #RogerThomas one.
Go to 'Window' -> 'Preferences' -> 'Java' -> 'Editor' -> 'Hovers'
Then check 'Variable Values'.
If the 'Variable Values' option collides with 'Combined Hover', uncheck last one or configure new keys for it.

Categories