dd619 dd619 - 3 months ago 14
Java Question

Customize catch block in Java and enable smart insert in Eclipse

In Eclipse when we select code statements and right click on it, it gives us an option for surrounding with

try/catch
block, after selecting this option our code looks like.

try {
//selected code lines
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


Now I want to define a custom catch block which should look like below after selecting surround with
try/catch
block from eclipse smart insert:

try {
//selected code lines
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
myCustomLogger.LogMe(Log.getStackTraceString(e));
}


Is it possible?

Does Eclipse allow us to customize some properties for specific project in smart insert?

Answer

Yes it's possible, open Eclipse Preference and then Java > Code Style > Code Templates. There you select Code > Catch block body. Here you can edit your try/catch block.

Add myCustomLogger.LogMe(Log.getStackTraceString(${exception_var}));

enter image description here