magritte
magritte

Reputation: 7636

Eclipse Formatting Try/Catch Blocks with Braces on Separate Lines

I'm trying to set up Eclipse so that it formats a try catch finally block (when pressing CTRL+SHIFT+F as follows:

try
{
    // some code
}
catch (IOException exception)
{
    // some exception handling code
}
finally
{
   // some more code
}

I found the formatting options under Window >> Preferences >> Java >> Code Style >> Formatter and added a new Active profile, however I can't see an option for try catch blocks on the braces tab. I've set everything on that tab to "Next line", however the code now formats as:

try
{
    // some code
} catch (IOException exception)
{
   // some exception handling code  
} finally
{
   // some more code
}

Upvotes: 4

Views: 4730

Answers (3)

Le Quang Hai
Le Quang Hai

Reputation: 11

You may go to this https://help.eclipse.org/latest/index.jsp?topic=%2Forg.eclipse.jdt.doc.isv%2Freference%2Fapi%2Forg%2Feclipse%2Fjdt%2Fcore%2Fformatter%2FDefaultCodeFormatterConstants.html then hit Ctrl + F searching for the phrases below:

"org.eclipse.jdt.core.formatter.insert_new_line_before_catch_in_try_statement"
"org.eclipse.jdt.core.formatter.insert_new_line_before_finally_in_try_statement"

You may find out how to configure the settings. If not, here is the xml file that I used:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<profiles version="13">
    <profile kind="CodeFormatterProfile" name="myProfile" version="13">
        <setting id="org.eclipse.jdt.core.formatter.insert_new_line_before_catch_in_try_statement" value="insert" />
        <setting id="org.eclipse.jdt.core.formatter.insert_new_line_before_else_in_if_statement" value="insert" />
        <setting id="org.eclipse.jdt.core.formatter.insert_new_line_before_finally_in_try_statement" value="insert" />

Upvotes: 1

Francis Upton IV
Francis Upton IV

Reputation: 19443

You have to do it in two places:

  1. The Braces tab under "Blocks", set to "Next Line".
  2. In the Control Statements tab, check the two boxes related to try/catch.

Upvotes: 7

mavroprovato
mavroprovato

Reputation: 8362

The settings you need are in the Control Statements tab:

  • Insert a new line before 'catch' in a 'try' statement
  • Insert a new line before 'finally' in a 'try' statement

Upvotes: 9

Related Questions