Recent

Author Topic: IDE auto-complete try except/finally end structure  (Read 6253 times)

Jkey

  • New Member
  • *
  • Posts: 44
IDE auto-complete try except/finally end structure
« on: August 30, 2016, 09:04:39 am »
Sometimes I need to use Eclipse for some Java programming, and there's an auto-complete feature what I like very much. If I enter the following:
Code: Java  [Select][+][-]
  1. File f = new File(filepath);
  2. f_in.close();
then Eclipse gives me warnings about possible unhandled exceptions. With a simple mouse click I can surround these commands with try/catch statements automatically and fill the catch part like this:
Code: Java  [Select][+][-]
  1. File f = new File(filepath);
  2. try {
  3.   f_in = new FileInputStream(f);
  4.   f_in.close();
  5. } catch (FileNotFoundException e) {
  6.     e.printStackTrace();
  7.     Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
  8. } catch (IOException e) {
  9.     e.printStackTrace();
  10.     Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
  11. }

I use Lazarus more often than Eclipse and I personally hate Java programming, but I like this particular feature. I'm wondering if something similar possible with Lazarus IDE? I know that there is already an auto-complete feature for begin-end; but I don't have any information about a try except/finally end; feature. Similarly if I write the following:
Code: Pascal  [Select][+][-]
  1. var StrList: TStringList;
  2. begin
  3.   StrList := TStringList.Create;
  4.   StrList.LoadFromFile(filepath);
  5.   StrList.SaveToFile(filepath);
  6.   StrList.Free;
  7. end;
the IDE could suggest me for LoadFromFile and SaveToFile a try/except/finally block, and I can fill both the except and finally part with something like this:
Code: Pascal  [Select][+][-]
  1. var StrList: TStringList;
  2. begin
  3.   StrList := TStringList.Create;
  4.   try
  5.     try
  6.       StrList.LoadFromFile(filepath);
  7.       StrList.SaveToFile(filepath);
  8.     except on e: Exception do ShowMessage(e.ClassName + ' error raised, with message: ' + e.Message);
  9.     end;
  10.   finally
  11.     StrList.Free;
  12.   end;
  13. end;
Is this feature available in current Lazarus IDE, and I simply didn't notice it? If yes, how can I invoke it?

Leledumbo

  • Hero Member
  • *****
  • Posts: 8746
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: IDE auto-complete try except/finally end structure
« Reply #1 on: August 30, 2016, 09:35:22 am »
Exceptions in Java is a contract, if you don't handle it, you must mark the method with such exception and it will be checked by the compiler for consistency. Exceptions in Object Pascal is not a contract, i.e. the compiler doesn't note and check what possible exceptions may be raised in a method body, therefore such a feature is not possible from compiler POV. There is possibility, but that needs (probably huge) improvements to the codetools. Surrounding the code with try-finally / try-except, however, is available via refactoring context menu. Just select a block, open context menu->refactoring->pick whatever you want to surround the code with.

Jkey

  • New Member
  • *
  • Posts: 44
Re: IDE auto-complete try except/finally end structure
« Reply #2 on: August 30, 2016, 02:50:03 pm »
Thanks for the explanation.

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: IDE auto-complete try except/finally end structure
« Reply #3 on: August 30, 2016, 06:09:02 pm »
You can design a code template that does most of what you want the Lazarus IDE to do.
If you back up your lazarus.dci file (in the /lazarus/config/ directory) and replace it with the unzipped attachment, you can then type

strlistload

in the editor, followed by a space, and the code you want will be inserted at the current cursor position. Of course you can adapt the code template to do more exactly what might suit you.

rc.1990

  • Jr. Member
  • **
  • Posts: 54
Re: IDE auto-complete try except/finally end structure
« Reply #4 on: August 30, 2016, 06:15:00 pm »
Just curious why try/except and try/finally that are two commands can't also be a unique command such as try/except/finally?

For example, instead of having:
Code: Pascal  [Select][+][-]
  1. var StrList: TStringList;
  2. begin
  3.   StrList := TStringList.Create;
  4.   try
  5.     try
  6.       StrList.LoadFromFile(filepath);
  7.       StrList.SaveToFile(filepath);
  8.     except
  9.       on e: Exception do ShowMessage(e.ClassName + ' error raised, with message: ' + e.Message);
  10.     end;
  11.   finally
  12.     StrList.Free;
  13.   end;
  14. end;
  15.  

Why a unique command try/except/finally is not possible?
Code: Pascal  [Select][+][-]
  1. var StrList: TStringList;
  2. begin
  3.   StrList := TStringList.Create;
  4.   try
  5.     StrList.LoadFromFile(filepath);
  6.     StrList.SaveToFile(filepath);
  7.   except
  8.     on e: Exception do ShowMessage(e.ClassName + ' error raised, with message: ' + e.Message);
  9.   finally
  10.     StrList.Free;
  11.   end;
  12. end;
  13.  

Thaddy

  • Hero Member
  • *****
  • Posts: 14201
  • Probably until I exterminate Putin.
Re: IDE auto-complete try except/finally end structure
« Reply #5 on: August 30, 2016, 06:42:50 pm »
They are two completely different things.
The Try / Finally is about memory management for heap allocated objects. If you come from java you are spoiled. (and not very well educated if you think it is better)
The try / Except is about exceptions / error trapping. Just like Java.

Compared to java, which is in all but name a scripting language (I do NOT mean Java Script, properly named ECMA script) / ok, bit too strong, FreePascal renders real processor  level compiled code and needs to take care of memory allocation and de-allocation itself. That's where try/finally comes in.
Java does that for you by means of its garbage collector. In many ways similar strategies are possible in FPC, like ARC and the LLVM model (which is also ARC, at least ARC like).
That is not exactly the same however.
finally and except are different things, remember that.

FYI: I use Java a lot! I am not bashing it.

Anyway, my first Pascal compiler compiled to P-code as well: UCSD Pascal.
I also know C++ allows this somehow but not that I like that: mixing up paradigms.













« Last Edit: August 30, 2016, 06:49:54 pm by Thaddy »
Specialize a type, not a var.

Leledumbo

  • Hero Member
  • *****
  • Posts: 8746
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: IDE auto-complete try except/finally end structure
« Reply #6 on: August 30, 2016, 09:13:41 pm »
Just curious why try/except and try/finally that are two commands can't also be a unique command such as try/except/finally?
Legacy.
Why a unique command try/except/finally is not possible?
It is possible, but the compiler developers aren't convinced enough why it should be implemented. Please read this wiki page along with the linked FAQ page to understand the criteria of a feature proposal that can be accepted (not necessarily implemented). From what I can see, the unified syntax only reduces typing, with the expense of misunderstanding the flow (When will finally block be executed? In case exception happens, will it be executed as well? Before or after the except block? Etc.). With separated syntax, you can clearly design the flow yourself with nesting. except inside or finally inside? You choose.

rc.1990

  • Jr. Member
  • **
  • Posts: 54
Re: IDE auto-complete try except/finally end structure
« Reply #7 on: August 31, 2016, 05:27:30 am »
Leledumbo and Thaddy, thanks for the explanation.

 

TinyPortal © 2005-2018