[ ??? ] FileOpen() Exception

Discuss the future of the AutoHotkey language
_3D_
Posts: 251
Joined: 29 Jan 2014, 14:40

[ ??? ] FileOpen() Exception

14 Feb 2020, 10:44

It is madness:

Code: Select all

#SingleInstance Force
comm:= FileOpen('\\.\COM1', "w")
When serial (com1) is taken by another program FileOpen() void exception Access Denied and comm variable keep its value.

It is in times easier FileOpen() to return 0 or ""

Code: Select all

#SingleInstance Force
if comm:= FileOpen('\\.\COM1', "w") {
	;file actions
}
I understand exceptions when Read/Write to file but this is true madness like in Java.

Code: Select all

#SingleInstance Force
comm:= 'madness'
try if comm:= FileOpen('\\.\COM1', "w") {
	;file actions
}
catch
	MsgBox('access denied')
MsgBox(type(comm) ' -> ' comm)
AHKv2.0 use the future now.
User avatar
nnnik
Posts: 4379
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: [ ??? ] FileOpen() Exception

14 Feb 2020, 13:58

This is what should happen.
I don't see how anything else should happen.
There will be no special circumstances for 500 unique cases just because someone in the forum finds it convenient.
A user writing a script needs to be informed when a command doesnt work.
Exceptions are the way of doing this.
Recommends AHK Studio
guest3456
Posts: 2979
Joined: 09 Oct 2013, 10:31

Re: [ ??? ] FileOpen() Exception

14 Feb 2020, 14:41

nnnik wrote:
14 Feb 2020, 13:58
A user writing a script needs to be informed when a command doesnt work.
Exceptions are the way of doing this.
returning 0 or "" is also a way to be informed when a command/func doesnt work

User avatar
kczx3
Posts: 993
Joined: 06 Oct 2015, 21:39

Re: [ ??? ] FileOpen() Exception

14 Feb 2020, 16:27

guest3456 wrote:
14 Feb 2020, 14:41
nnnik wrote:
14 Feb 2020, 13:58
A user writing a script needs to be informed when a command doesnt work.
Exceptions are the way of doing this.
returning 0 or "" is also a way to be informed when a command/func doesnt work
Though FAR less informative.
User avatar
nnnik
Posts: 4379
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: [ ??? ] FileOpen() Exception

15 Feb 2020, 01:54

Far less intuitive for new users.
Recommends AHK Studio
_3D_
Posts: 251
Joined: 29 Jan 2014, 14:40

Re: [ ??? ] FileOpen() Exception

15 Feb 2020, 03:39

It is especially for these that liked exceptions around the simple code: OpenFile function
I think ALL is based on this nature of things. (NO EXCEPTIONS AT ALL)
Must read >>> KISS principle

Enjoy!
AHKv2.0 use the future now.
Helgef
Posts: 4102
Joined: 17 Jul 2016, 01:02
Contact:

Re: [ ??? ] FileOpen() Exception

15 Feb 2020, 03:45

Your examples are wrong, this is how to handle fileopen failure,

Code: Select all

try comm:= FileOpen('\\.\COM1', "w")
catch {
	; handle failure
}
;file actions
this is how to handle it with your suggestion,

Code: Select all

if comm:= FileOpen('\\.\COM1', "w") {
	;file actions
} else {
	; handle failure
}
But with your suggestion this code will never be safe,

Code: Select all

comm:= FileOpen('\\.\COM1', "w")
;file actions
It is now, you'll be notified if it fails.

Cheers.
_3D_
Posts: 251
Joined: 29 Jan 2014, 14:40

Re: [ ??? ] FileOpen() Exception

15 Feb 2020, 04:30

Helgef wrote:
15 Feb 2020, 03:45

Code: Select all

comm:= FileOpen('\\.\COM1', "w")
;file actions
It is now, you'll be notified if it fails.
Actually this code is that I need - one single line that return HANDLE or 0 if FileOpen fail and A_LastError is the place that I can see what happens. It because opening / acting / closing must be different procedures.
HANDLE is the thing. Exception give nothing neither to me nor to anyone else just break program flow.
FileOpen with exception:

Code: Select all

try comm:= FileOpen('\\.\COM1', 'w')
catch
  comm:= 0
Instead of a single line.
What if Programmer or User see: The system cannot find the file specified. (exception if no any serial interface present)
Will Programmer or User can add that serial interface? Probably NO.
Will Programmer or User can activate not shared resource? Probably NO.
What useful information is this?
AHKv2.0 use the future now.
Helgef
Posts: 4102
Joined: 17 Jul 2016, 01:02
Contact:

Re: [ ??? ] FileOpen() Exception

15 Feb 2020, 04:46

Fileopen returns A fileobject, not a handle. If it returned 0 you still would have to handle it, or you would get a less descriptive error message when you tried to invoke any of the fileobject methods or properties on the 0 value. If you really want to return 0, wrap the function, myfileopen().

Cheers.
User avatar
nnnik
Posts: 4379
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: [ ??? ] FileOpen() Exception

15 Feb 2020, 04:59

So you mean to say that you will just use A_LastError instead of ErrorLevel in v2?
One of the biggest issues of v1 was ErrorLevel and the impossibility to properly debug it.
v2 fixes this by throwing an Error whenever an Error occurs - to keep it stupid simple.

This also means that if you use any kind of method on a "" or a 0 it will throw an exception.
Then the user has to wonder where that value came from.
To keep it stupid simple we just throw whenever an error occurs.

In any case:

Code: Select all

try comm:= FileOpen('\\.\COM1', 'w')
catch
  comm:= 0
Why would you do this. What kind of benefit does that give you?
I just don't see any of your arguments actually applying to reality.
You just mention keywords that actually do not defend your point.
Instead of a single line.
What if Programmer or User see: The system cannot find the file specified. (exception if no any serial interface present)
Will Programmer or User can add that serial interface? Probably NO.
Will Programmer or User can activate not shared resource? Probably NO.
What useful information is this?
It is far better than getting the error:
This value of type "Integer" has no method named "..."
Informing the user of what is going wrong with your script and pointing towards solutions that way is a very good practice.
Imagine if the user has a COM interface connected via USB?
What if they just forgot to plug it in. Without any kind of error message the user will probably search for a long time before actually detecting that error.
With the error message that the COM interface is missing they will probably check that first.
Do I really have to explain to you how important user feedback is?
Recommends AHK Studio
_3D_
Posts: 251
Joined: 29 Jan 2014, 14:40

Re: [ ??? ] FileOpen() Exception

15 Feb 2020, 05:17

Helgef wrote:
15 Feb 2020, 04:46
Fileopen returns A fileobject, not a handle. If it returned 0 you still would have to handle it, or you would get a less descriptive error message when you tried to invoke any of the fileobject methods or properties on the 0 value. If you really want to return 0, wrap the function, myfileopen().

Cheers.
I am aware from the next code:

Code: Select all

comm:= FileOpen('\\.\COM1', 'w') ;The system cannot find the file specified.
comm.WriteLine('Hello world')
comm.Close()
The code must be look like:

Code: Select all

fileobject:= ''
preprocess(fileobject)
; ...
actprocess(fileobject)
; ...
endprecess(fileobject)

preprocess(ByRef fileobject) {
	;...
	try 
	{	fileobject:= FileOpen('\\.\COM1', 'w')
		;...
	} catch e { 
		fileobject:= ''
		OutputDebug e.message
}	}
actprocess(ByRef fileobject) {
	try 
	{	;...
		fileobject.WriteLine('Hello world')
	}
	catch e {
		;what to do on exception
		;probably Ill will try later or Ill close fileobject
		OutputDebug e.message
}	}
endprecess(ByRef fileobject) {
	try fileobject.Close()
	catch e
		OutputDebug e.message
}
Instead of this:

Code: Select all

fileobject:= ''
preprocess(fileobject)
; ...
actprocess(fileobject)
; ...
endprecess(fileobject)

preprocess(ByRef fileobject) {
	;...
	fileobject:= FileOpen('\\.\COM1', 'w')
}
actprocess(ByRef fileobject) {
	try 
	{	;...
		fileobject.WriteLine('Hello world')
	}
	catch e {
		;what to do on exception
		;probably Ill will try later or Ill close fileobject
		OutputDebug e.message
}	}
endprecess(ByRef fileobject) {
	if type(fileobject) == 'File' ;probably still File
		fileobject.Close()
}
AHKv2.0 use the future now.
User avatar
nnnik
Posts: 4379
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: [ ??? ] FileOpen() Exception

15 Feb 2020, 05:26

No you would use try whenever it is needed and makes sense:

Code: Select all

try {
	preprocess(fileobject)
	; ...
	actprocess(fileobject)
	; ...
	endprecess(fileobject)
} Catch e {
	if (e.message ~= "Access Denied") {
		logFeedback("Couldnt open COM: Access denied - This often happens if there is another program open that tries to access the COM port or this program is opened twice") 
	} ;...
}
Recommends AHK Studio

Return to “AutoHotkey v2 Development”

Who is online

Users browsing this forum: No registered users and 29 guests