Forum > Options

IDE auto-complete try except/finally end structure

(1/2) > >>

Jkey:
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  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---File f = new File(filepath);FileInputStream f_in = new FileInputStream(f);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  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---File f = new File(filepath);FileInputStream f_in;try {  f_in = new FileInputStream(f);  f_in.close();} catch (FileNotFoundException e) {    e.printStackTrace();    Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();} catch (IOException e) {    e.printStackTrace();    Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();}
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  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---var StrList: TStringList;begin  StrList := TStringList.Create;  StrList.LoadFromFile(filepath);  StrList.SaveToFile(filepath);  StrList.Free;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  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---var StrList: TStringList;begin  StrList := TStringList.Create;  try    try      StrList.LoadFromFile(filepath);      StrList.SaveToFile(filepath);    except on e: Exception do ShowMessage(e.ClassName + ' error raised, with message: ' + e.Message);    end;  finally    StrList.Free;  end;end;Is this feature available in current Lazarus IDE, and I simply didn't notice it? If yes, how can I invoke it?

Leledumbo:
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:
Thanks for the explanation.

howardpc:
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:
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  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---var StrList: TStringList;begin  StrList := TStringList.Create;  try    try      StrList.LoadFromFile(filepath);      StrList.SaveToFile(filepath);    except       on e: Exception do ShowMessage(e.ClassName + ' error raised, with message: ' + e.Message);    end;  finally    StrList.Free;  end;end; 
Why a unique command try/except/finally is not possible?

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---var StrList: TStringList;begin  StrList := TStringList.Create;  try    StrList.LoadFromFile(filepath);    StrList.SaveToFile(filepath);  except     on e: Exception do ShowMessage(e.ClassName + ' error raised, with message: ' + e.Message);  finally    StrList.Free;  end;end; 

Navigation

[0] Message Index

[#] Next page

Go to full version