AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Error Handling

 
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Ask for Help
View previous topic :: View next topic  
Author Message
Guest






PostPosted: Mon Nov 16, 2009 10:35 am    Post subject: Error Handling Reply with quote

Is it possible to handle errors just like what we do in other programming languages where we use Try and Catch or Finalise methods to handle errors...................so does autohotkey also have something like this???
Back to top
rtcvb32



Joined: 17 Feb 2008
Posts: 125

PostPosted: Mon Nov 16, 2009 10:47 am    Post subject: Reply with quote

At this time i am un-aware of anything like that. This is not a OO language; you'll have to go Old-School, back when C used errno, and values returned from the functions.

Code:

//C code example
FILE *ptr;
ptr = fopen("somefile");
if (!ptr){
//failed to open file.
//errno may contain extra information

}

;AHK example
FileRead, var, somefile
if ErrorLevel {
;failed with the file for some reason.
}


Also to note. Try/Catch does checks over every function and returns in the background, and it's a lot more expensive resources and CPU wise.

In reality, the code isn't that complicated that it needs that many checks or much for error catching like in Java. If there's a possibility for an error, check for it.
Back to top
View user's profile Send private message Yahoo Messenger
Guest






PostPosted: Mon Nov 16, 2009 10:53 am    Post subject: Reply with quote

thanks dude well wat if their is some error while running a .ahk script but still i want to continue by skipping the error
Back to top
rtcvb32



Joined: 17 Feb 2008
Posts: 125

PostPosted: Mon Nov 16, 2009 10:57 am    Post subject: Reply with quote

Anonymous wrote:
thanks dude well what if their is some error while running a .ahk script but still i want to continue by skipping the error


What kind of errors are you talking about? Sometimes the error will refuse to let the script work, in that case, debug and fix it.

Other errors are not so obvious, like a badly written Regex.

if you have code that's giving you trouble, paste it. There are a lot of people who are willing to pitch a few minutes in to help, like me for example. Smile
Back to top
View user's profile Send private message Yahoo Messenger
Guest






PostPosted: Mon Nov 16, 2009 11:00 am    Post subject: Reply with quote

well i was just asking to get more information abt the tool...ok for an example if their is a command which opens a file and if someone has deleted the file then obviously when the script runs it fails to execute or run the file(as it is deleted)...so wat to do nw in this case in order to skip a error like this and let the script execute the other commands after this following error....any idea???
Back to top
rtcvb32



Joined: 17 Feb 2008
Posts: 125

PostPosted: Mon Nov 16, 2009 11:11 am    Post subject: Reply with quote

Anonymous wrote:
well i was just asking to get more information about the tool...ok for an example if their is a command which opens a file and if someone has deleted the file then obviously when the script runs it fails to execute or run the file(as it is deleted)...so what to do now in this case in order to skip a error like this and let the script execute the other commands after this following error....any idea???


You need to fix your spelling.

If your OS locks the file, then perhaps you can still make use of it. If not, then it's gone. But the script will still want to do something.

Here's what you do. READ THE DOCUMENTATION. Each function has a good description not only what it does, what options there are, but possible error information in case this stuff happens. A little planning goes a long way.

In the case of FileRead, it states
Quote:

ErrorLevel is set to 0 if the load was successful. It is set to 1 if a problem occurred such as: 1) file does not exist; 2) file is locked or inaccessible; 3) the system lacks sufficient memory to load the file.


So simply by seeing this if Errorlevel >0 then an error happened, and depending on it's value we know how to react.

Code:

FileRead, var, somefile
if (ErrorLevel = 1)
    msgbox File does not exist
else if (ErrorLevel = 2)
    msgbox File is locked or inaccessible
else if (ErrorLevel = 3)
    msgbox Cannot open due to lack of resources
else {
;it's just fine, work with it.
}

;or
FileRead, var, somefile
if ErrorLevel
    return

;anything following, it read the file.


Last edited by rtcvb32 on Mon Nov 16, 2009 11:21 am; edited 1 time in total
Back to top
View user's profile Send private message Yahoo Messenger
rtcvb32



Joined: 17 Feb 2008
Posts: 125

PostPosted: Mon Nov 16, 2009 11:17 am    Post subject: Reply with quote

rtcvb32 wrote:
Here's what you do. READ THE DOCUMENTATION.


Maybe i was being to subtle...
Back to top
View user's profile Send private message Yahoo Messenger
Guest






PostPosted: Mon Nov 16, 2009 11:19 am    Post subject: Reply with quote

thanks a lot man its a good idea will work on it
Back to top
Display posts from previous:   
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Ask for Help All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group