0

Specifically what I want is when I use the quick fix "surround with try/catch" to not add extra newlines. "surround with try/catch" shows up as a quick fix option when you mouse over code which throws a exception. For example this code shows a quick fix option:

m_tsocket = new ServerSocket(port);

And it becomes this when "surround with try/catch" is used:

try {
    m_tsocket = new ServerSocket(port);
} catch (IOException e1) {}

What I want is for the "surround with try/catch" to give me this instead:

try {m_tsocket = new ServerSocket(port);} catch (IOException e1) {}

The point is I dont need the extra newlines except in rare cases. The newlines makes it take longer time to scroll through the code. Most of the code is inside the catch and I handle that after Ive done the general code structure.

L. Drake
  • 1
  • 1
  • 4
  • 3
    You want the `try...catch` on one line? why? – achAmháin Feb 08 '18 at 15:14
  • @notyou I think just a matter of taste – AhmadWabbi Feb 08 '18 at 15:16
  • 1
    IMO, that makes code less readable. In this case, the OP could just use backspace a few times whenever they use the quick fix. – achAmháin Feb 08 '18 at 15:18
  • I agree. But this is what the OP wants ... – AhmadWabbi Feb 08 '18 at 15:20
  • I thought the templates editor for `editor` would work, as it features a `try`/`catch` block with variables, but it looks like it only works with the reduntant "surround with `try_catch`", not with the default one. – Mena Feb 08 '18 at 15:21
  • 4
    Throwing away exceptions like that without reporting them is very poor practice. It will make your code much harder to debug when it goes wrong. – greg-449 Feb 08 '18 at 15:32
  • 1
    Unless the code is strictly and only for you, you should follow standard and recommended practices. That will help to make your code useful and long-lived. – LMC Feb 08 '18 at 15:48
  • @greg-449 When you say reporting I assume you mean e1.printStackTrace();? I took a look and it makes my logging easier. Thanks. Im not sure what you meant by throwing away exceptions since I said I handle the catch/exceptions later. – L. Drake Feb 08 '18 at 18:10
  • "The newlines makes it take longer time to scroll through the code." If this is a major problem for you, you are going the wrong way to solve it. Instead, focus on what code could be refactored to other methods or classes. Take care of the single responsibility principle. – Lukas Körfer Feb 08 '18 at 19:03
  • For testing you can use printStackTrace. A professional program would write the errors to a log. I said throwing away because you are catching the exception and then doing nothing with it - you are throwing away valuable information about the error. Eclipse can give a warning about empty {} blocks - turn it on. – greg-449 Feb 08 '18 at 20:05

1 Answers1

0

This is the only workaround I can think of, and it's a bit ugly.

  • Go to Window -> Preferences.
  • Search for Editor.
  • Unfold and select Templates.
  • Select the try_catch template and remove line breaks as you deem fit.
  • Apply and save.

Then, instead of using Eclipse's Surround with try-catch prompt...

  • Select your line(s) that throw(s) Exception(s)
  • Use the custom template: Shift-Alt-Z, then typically 7 (or whichever is the shortcut for try_catch).

The bottomline is, I do not believe the Surround with try catch default template is editable, but Eclipse (at least, Neon) does provide a template that is editable out of the box, which in turn, you can edit.

Edit

As mentioned by others, I really feel like pointing out the obvious, i.e. what you're trying to achieve is not a desirable practice. A caught exception should be handled somehow, and in its own line.

Mena
  • 47,782
  • 11
  • 87
  • 106
  • 1
    The fact that you need such a workaround suggests to me that they should be on different lines :) – achAmháin Feb 08 '18 at 15:27
  • @notyou well, I would never have them on the same line myself :D Only pointing out at the least kludgy way I can think of to do so... – Mena Feb 08 '18 at 15:28
  • 1
    @Mena I see. I think I will just try to instead pretend the newlines aren't there. – L. Drake Feb 08 '18 at 17:03