[ ??? ] FileOpen() Exception

Discuss the future of the AutoHotkey language
_3D_
Posts: 277
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 alpha forever.
User avatar
nnnik
Posts: 4500
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: 3454
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: 1640
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: 4500
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: 277
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 alpha forever.
Helgef
Posts: 4709
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: 277
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 alpha forever.
Helgef
Posts: 4709
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: 4500
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: 277
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 alpha forever.
User avatar
nnnik
Posts: 4500
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
_3D_
Posts: 277
Joined: 29 Jan 2014, 14:40

Re: [ ??? ] FileOpen() Exception

17 Feb 2020, 07:24

nnnik wrote:
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") 
	} ;...
}

Code: Select all

file:= FileOpen('*', 'w') ;Exception: The operation completed successfully.
Extremely useful message. (Now I am informed).
:bravo: https://lexikos.github.io/v2/docs/commands/FileOpen.htm
AHKv2.0 alpha forever.
User avatar
nnnik
Posts: 4500
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: [ ??? ] FileOpen() Exception

17 Feb 2020, 07:32

I get:

Code: Select all

---------------------------
AutoHotkey Script (neu) (6).ahk
---------------------------
Error:  Es wurde versucht, auf ein Token zuzugreifen, das nicht vorhanden ist.

Specifically: 1008

	Line#
--->	001: file := FileOpen('*', 'w')
	003: file.write("Test")
	004: file.read()
	005: Msgbox()
	006: Exit
	007: Exit
	007: Exit

The current thread will exit.
---------------------------
OK   
---------------------------
It was attempted to access a token which does not exist.
Recommends AHK Studio
_3D_
Posts: 277
Joined: 29 Jan 2014, 14:40

Re: [ ??? ] FileOpen() Exception

17 Feb 2020, 07:42

nnnik wrote:
17 Feb 2020, 07:32
I get:

Code: Select all

---------------------------
AutoHotkey Script (neu) (6).ahk
---------------------------
Error:  Es wurde versucht, auf ein Token zuzugreifen, das nicht vorhanden ist.

Specifically: 1008

	Line#
--->	001: file := FileOpen('*', 'w')
	003: file.write("Test")
	004: file.read()
	005: Msgbox()
	006: Exit
	007: Exit
	007: Exit

The current thread will exit.
---------------------------
OK   
---------------------------
It was attempted to access a token which does not exist.
My AHK_U64_a108:

Code: Select all

file:= FileOpen('*', 'w')
---------------------------
FileOpen.ahk
---------------------------
Error:  The operation completed successfully.

Specifically: 0

	Line#
--->	001: file := FileOpen('*', 'w')
	002: Exit
	003: Exit
	003: Exit

The current thread will exit.
---------------------------
OK   
---------------------------
AHKv2.0 alpha forever.
User avatar
kczx3
Posts: 1640
Joined: 06 Oct 2015, 21:39

Re: [ ??? ] FileOpen() Exception

17 Feb 2020, 08:01

I get the same as @_3D_. That's definitely a bug.
User avatar
nnnik
Posts: 4500
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: [ ??? ] FileOpen() Exception

17 Feb 2020, 08:19

The fact that the language is different on my PC probably mean that this is not an AHK error message but rather a Windows error message.
We have to decide whether we want to fix windows bugs in AHK with possible hackfixes.
Recommends AHK Studio
User avatar
kczx3
Posts: 1640
Joined: 06 Oct 2015, 21:39

Re: [ ??? ] FileOpen() Exception

17 Feb 2020, 08:53

nnnik wrote:
17 Feb 2020, 08:19
The fact that the language is different on my PC probably mean that this is not an AHK error message but rather a Windows error message.
We have to decide whether we want to fix windows bugs in AHK with possible hackfixes.
I don't see how that is. AHK is incorrectly interpreting the return code as an error. Probably has something to do with the recent HRESULT handling changes.
User avatar
nnnik
Posts: 4500
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: [ ??? ] FileOpen() Exception

17 Feb 2020, 09:01

From what I understand nothing is connected to the stdout - you would have to call AlloCConsole first.
See the example in the help file:

Code: Select all

; Open a console window for this demonstration:
DllCall("AllocConsole")
; Open the application's stdin/stdout streams in newline-translated mode.
stdin  := FileOpen("*", "r `n")  ; Requires [v1.1.17+]
stdout := FileOpen("*", "w `n")
; For older versions:
;   stdin  := FileOpen(DllCall("GetStdHandle", "int", -10, "ptr"), "h `n")
;   stdout := FileOpen(DllCall("GetStdHandle", "int", -11, "ptr"), "h `n")
stdout.Write("Enter your query.`n\> ")
stdout.Read(0) ; Flush the write buffer.
query := RTrim(stdin.ReadLine(), "`n")
stdout.WriteLine("Your query was '" query "'. Have a nice day.")
stdout.Read(0) ; Flush the write buffer.
Sleep 5000
Which doesn't throw an error for me.
The error is actually an error - the description, probably acquired through some windows function, is wrong though.
Do we need to fix descriptions that are incorrectly given by Windows and probably differ from windows to windows version?
Recommends AHK Studio
User avatar
kczx3
Posts: 1640
Joined: 06 Oct 2015, 21:39

Re: [ ??? ] FileOpen() Exception

17 Feb 2020, 09:32

I see. Thanks for clearing up the part about needing AllocConsole. I don't work with stdin/stdout ever in AHK really.

Basically, it looks like GetStdHandle returns NULL if there hasn't been a console allocated. TextFile::_Open then returns false from TextIO.cpp. If I manually do this in AHK, then I get something that makes more sense to me.

Code: Select all

fHandle := DllCall("GetStdHandle", "int", -10, "ptr")
fileObj := FileOpen(fHandle, "h `n")

Code: Select all

---------------------------
Temp.ahk
---------------------------
Error:  The handle is invalid.

Specifically: 6

	Line#
	003: fHandle := DllCall("GetStdHandle", "int", -10, "ptr")
--->	004: fileObj := FileOpen(fHandle, "h 
")
	006: MsgBox("Last Error: " A_LastError "
ErrorLevel: " ErrorLevel "
fHandle: " Type(fHandle) " - " fHandle)
	007: Exit
	008: Exit
	008: Exit

The current thread will exit.
---------------------------
OK   
---------------------------

Return to “AutoHotkey Development”

Who is online

Users browsing this forum: No registered users and 44 guests