Recent

Author Topic: IDE auto-complete try except/finally end structure  (Read 6942 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: 8766
  • 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: 15200
  • Censorship about opinions does not belong here.
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 »
Of course the national anthem of the U.S.A. was written by Jimi Hendrix, didn't you know that?

Leledumbo

  • Hero Member
  • *****
  • Posts: 8766
  • 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.

tol

  • New member
  • *
  • Posts: 8
Re: IDE auto-complete try except/finally end structure
« Reply #8 on: March 30, 2024, 11:48:47 pm »
Sorry, this thread is already old, but I still have to correct something here.

@Thaddy

Accusing someone of not being well educated is an impudence in itself.
But then to claim Java is a scripting language is ridiculous.
Scripting languages usually have an interpreter and are not based on compilation!

If the compiler compiles to byte code it is in fact a compiled language. Period!
Just because Java doesn't (usually!) compile to native machine code doesn't make it a "scripting language"!
And because there are/were (old) interpreted implementations, the language is by no means a "script language". The same applies to other languages like C#, LUA etc.
In the end, “script language” is already a weak definition. You can script with many languages. As so often it depends on the specific use!

The opposite of compiling is by no means “scripting”.

Try to catch up with your own education starting here: https://en.wikipedia.org/wiki/Scripting_language

Unfortunately, you can always see that some of the remaining Pascal enthusiasts are "extremist" when it comes to "their" language.

PS: Saying that you use something and therefore don't discredit it.. is an imposition on the reader in itself. It just shows that this accusation has clearly been made many times!

cdbc

  • Hero Member
  • *****
  • Posts: 1355
    • http://www.cdbc.dk
Re: IDE auto-complete try except/finally end structure
« Reply #9 on: March 31, 2024, 09:30:35 am »
A bit of "Necromancy" going on here?!?
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 2.2.6 up until Jan 2024 from then on it's: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 3.0

 

TinyPortal © 2005-2018