When I define a new exeption on IntelliJ IDEA Ultimate and use ctlr enter to create the exception class, it extends Throwable by default, I would like to change that to exception but I can't find where it is in the settings.
I've tried to to the settings in Editor -> File and Code Templates but I can't find what I'm searching for.
CodePudding user response:
Overriding default behavior for creating new exception classes in not available yet for IntelliJ IDEA. Here is a feature request.
Still it is possible to use LiveTemplates.
- In the main menu, go to File > Settings (or Preferences on macOS).
- In the Settings/Preferences dialog, navigate to the Editor > Live Templates section.
- In the right pane, find "Java" item and expand it.
- Add new entry, e.g. "exc" (or any symbols on your choice).
- In the editor window, modify the template code as needed to change the default behavior.
For example, to make the created exception class extend the Exception class instead of Throwable modify the code as follows:
public class $NAME$ extends Exception {
public $NAME$() {
}
public $NAME$(String message) {
super(message);
}
public $NAME$(String message, Throwable cause) {
super(message, cause);
}
public $NAME$(Throwable cause) {
super(cause);
}
protected $NAME$(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
}
After making the desired changes, click Apply
button to save changes.
Usage:
- Create a class file
- Remove contents except for "package ..." line.
- Put cursor somewhere, type exc, push
Tab
key. - The template will be expanded and the cursor will be at the position where you have to type the name of the exception class.