Jindent - Java Source Code Formatter http://www.jindent.com
 



title
5.6.2.3.8 Try/Catch

Brace style for try-catch statements

Brace style for try-catch statements

Left brace { new line


Controls whether the left brace of try-catch statements appears in a new line or not.


Setting left brace to a new line:

try                                     
{                                       
    ...                                 


Do not set left brace to a new line:

try {                                   
    ...                                 



Right brace } new line


Controls whether the right brace of try-catch statements appears in a new line or not.


Setting right brace to a new line:

try                                     
{                                       
    ...                                 
}                                       
catch(int e)                            
{                                       
    ...                                 
}                                       


Do not set right brace to a new line:

try                                     
{                                       
    ...                                 
catch(int e)                          
{                                       
    ...                                 
}                                       



Indent left brace {


Specifies the indentation size of left braces.


Setting left brace to a new line and using an indentation size of 4:

try                                     
····{                                   
····...                                 


Setting left brace to a new line and using an indentation size of 0:

try                                     
{                                       
    ...                                 


Do not set left brace to a new line and use indentation size of 1:

try·{                                   
    ...                                 


Do not set left brace to a new line and use indentation size of 0:

try{                                    
    ...                                 



Indent right brace }


Specifies the indentation size of right braces.


Left brace is on a new line and set indentation size of 4 for left and right braces:

try                                     
····{                                   
····...                                 
····}                                   
catch(int·e)·                           
····
{                                   
····...                                 
····}                                   


Left brace is on a new line and set indentation size of 0 for left and right braces:

try                                     
{                                       
····...                                 
}                                       
catch(int·e)·                           
{                                       
····...                                 
}                                       



Indent after right brace }


Controls indentation of right braces in try-catch statements.


Do not set right brace to a new line and use an indentation size of 0:

try                                     
{                                       
    ...                                 
}
catch(int e)                           
{                                       
    ...                                 
}                                       


Do not set right brace to a new line and use an indentation size of 1:

try                                     
{                                       
    ...                                 
catch(int e)                          
{                                       
    ...                                 
}                                       



Cuddle braces of empty blocks {}


Specifies how to format braces of empty try-catch statement blocks.


Cuddle braces of empty blocks:

try {                                   
    ...                                 
catch(int e) {}                       


Do not cuddle braces of empty blocks:

try {                                   
    ...                                 
catch(int e) {                        
}                                       



Indent cuddled braces {}


Controls the indentation size of cuddled braces.


Cuddle braces of empty blocks and use an indentation of 0:

try·{                                   
    ...                                 
}
·catch(int·e){}                        


Cuddle braces of empty blocks and use an indentation of 1:

try·{                                   
    ...                                 
}
·catch(int·e)·{}                       



Prohibit blank lines after left brace {


Prohibits blank lines directly after a left brace of try-catch statement blocks.

Some code conventions require a blank line before each comment.
Such a formatting looks proper if the comment, for instance, appears between two statements.
But if a comment follows directly after a left brace of try-catch statement blocks an unecessary gap can appear.


Left brace of statement block is set to a new line using an indentation size of 0.
Additionally a blank line shall be inserted before comments:

try                                     
{                                       
                                       
    
// comment with a preceding blank line
    
callAnotherMethod();                
                                       
    
// another comment                  
    
...                                 
catch(int e) {                        
    ...                                 
}                                       


In the example above the left brace and blank line before the comment seem to create an unnecessary gap.
To avoid such a gap just prohibit blank lines after left braces:

try                                     
{                                       
    
// comment with a preceding blank line
    
callAnotherMethod();                
                                       
    
// another comment                  
    
...                                 
catch(int e) {                        
    ...                                 
}                                       

Now the blank line before the first comment disappeared and closed the gap, but all other comments still contain the expected blank line as usual.
Of course this works for all kind of blank lines and not only for blank lines before comments.

See also... See also: Jindent - Settings - Formatter - C / C++ - Blank Lines - Comments , Jindent - Settings - Formatter - C / C++ - Blank Lines




If number of lines in body is at least ... then insert blank line after {


Inserts an extra blank line after left braces of try-catch statements containing a certain number of lines in their bodies.

Code conventions which prefer the K&R brace style (also known as Kernal, Cpp or Sun Style: do NOT put left brace to a new line) can run into a specific issue regarding readability of source code.


Let's assume the following source code example is formatted in a typical K&R brace style:

try {                                   
    callMethod();                       
catch(int e)                          
    cerr << e;                          
}                                       
                                       
try {                                   
    callMethodA(i);                     
    
for (int i = 0; i < n; i++) {       
        callMethodB(i);                 
        callMethodC(i);                 
    }                                   
    callMethodD(i);                     
catch(int e) {                        
    
// print error                      
    
cerr << e;                          
}                                       

In this case formatting of first try-catch statement looks fine, but the second try-catch statement seems to be formatted too close: The try { statement the first block statement callMethodA(i); are connected too close and could cause confusion.
To avoid a too close formatting for specific try-catch statements use setting "If number of lines in body is at least ... then insert blank line after {" to insert an extra blank line before the first block statement.


The same example as above, but now using setting "If number of lines in body is at least ... then insert blank line after {" with a value of 2.

try {                                   
    callMethod();                       
catch(int e)                          
    cerr << e;                          
}                                       
                                       
try {                                   
                                       
    callMethodA(i);                     
    
for (int i = 0; i < n; i++) {       
        callMethodB(i);                 
        callMethodC(i);                 
    }                                   
    callMethodD(i);                     
catch(int e) {                        
                                       
    
// print error                      
    
cerr << e;                          
}                                       

Now formatting of the second try-catch statement seems to be much clearer and the first try-catch statement is still formatted as before.
This setting counts the lines of code in every try-catch statement and if at least 2 lines are counted then an extra blank line is inserted after the left brace of try-catch statement block.
With this setting typical small try-catch statements can be kept compact, but try-catch statements with an extended body are formatted more spacious and clearer.


The same example again, but ""If number of lines in body is at least ... then insert blank line after {"" is set to value 3.

try {                                   
    callMethod();                       
catch(int e)                          
    cerr << e;                          
}                                       
                                       
try {                                   
                                       
    callMethodA(i);                     
    
for (int i = 0; i < n; i++) {       
        callMethodB(i);                 
        callMethodC(i);                 
    }                                   
    callMethodD(i);                     
catch(int e) {                        
    
// print error                      
    
cerr << e;                          
}                                       

Now the catch part of the second statement is formatted in a more compact way too.


Using value 0:

try {                                   
                                       
    callMethod();                       
catch(int e)                          
                                       
    cerr << e;                          
}                                       
                                       
try {                                   
                                       
    callMethodA(i);                     
    
for (int i = 0; i < n; i++) {       
        callMethodB(i);                 
        callMethodC(i);                 
    }                                   
    callMethodD(i);                     
catch(int e) {                        
                                       
    
// print error                      
    
cerr << e;                          
}                                       

And all try-catch statements are formatted more spacious.

To turn this setting completely off just choose value infinite.



If number of lines in body is at least ... then insert blank line before }


Inserts an extra blank line before right braces of statements/annotations/arrays containing a certain number of lines in their bodies.

This setting works exactly in the same way as "If number of lines in body is at least ... then insert blank line after {", but now the extra blank line will be inserted at the end of statement/annotation/array bodies.


Let's assume "If number of lines in body is at least ... then insert blank line after {" is set to 2 and "If number of lines in body is at least ... then insert blank line before }" is set to 2 too:

try {                                   
    callMethod();                       
catch(int e)                          
    cerr << e;                          
}                                       
                                       
try {                                   
                                       
    callMethodA(i);                     
    
for (int i = 0; i < n; i++) {       
        callMethodB(i);                 
        callMethodC(i);                 
    }                                   
    callMethodD(i);                     
                                       
catch(int e) {                        
                                       
    
// print error                      
    
cerr << e;                          
                                       
}                                       

This setting creates an even more spacious formatting style.



Do not insert blank line before single left brace


This setting is related to the previous setting: "If number of lines in body is at least ... then insert blank line before }"
If insertion of an extra blank line before right braces is used, then this setting prohibits this extra blank line insertion before a single right braces. A single right brace is a brace which appears in a single line without any other token:


try {                       // <- this right brace appears alone
    
callMethodA();                      
    callMethodB();                      
catch(int e) {      // <- this right brace does not appear alone
    
callMethodC();                      
    callMethodC();                      
}                           
// <- this right brace appears alone

The code example above shows that the right brace of } catch(int e) does not appear alone, it is directly followed by an catch token.

Now the next code example shows the usage of this setting:


Let's recall the example from the previous setting section. There we assumed "If number of lines in body is at least ... then insert blank line after {" is set to 2 and "If number of lines in body is at least ... then insert blank line before }" is set to 2 too.

try {                                   
    callMethod();                       
catch(int e)                          
    cerr << e;                          
}                                       
                                       
try {                                   
                                       
    callMethodA(i);                     
    
for (int i = 0; i < n; i++) {       
        callMethodB(i);                 
        callMethodC(i);                 
    }                                   
    callMethodD(i);                     
                                       
catch(int e) {                        
                                       
    
// print error                      
    
cerr << e;                          
                                       
}                                       

Actually this already looks nice spacious for the try block of the second try-catch statement, but for the catch block an extra blank line seems to be unnecessary.


Now we additionally use setting: "Do not insert blank line before single left brace" and prohibit extra blank lines before single right braces:

try {                                   
    callMethod();                       
catch(int e)                          
    cerr << e;                          
}                                       
                                       
try {                                   
                                       
    callMethodA(i);                     
    
for (int i = 0; i < n; i++) {       
        callMethodB(i);                 
        callMethodC(i);                 
    }                                   
    callMethodD(i);                     
                                       
catch(int e) {                        
                                       
    
// print error                      
    
cerr << e;                          
}                                       

Now the extra blank line was only inserted before } catch(int e) { because this is the only case a right braces does not appear as a single brace.

So we finally have a well formatted output where no uncessary extra blank lines extend the source code.




See also... See also: Jindent - Settings - Formatter - C / C++ - Braces Style - Presets