AutoHotkey Community

It is currently May 26th, 2012, 11:18 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 8 posts ] 
Author Message
 Post subject: Error Handling
PostPosted: November 16th, 2009, 11:35 am 
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???


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: November 16th, 2009, 11:47 am 
Offline

Joined: February 17th, 2008, 7:09 am
Posts: 536
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.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: November 16th, 2009, 11:53 am 
thanks dude well wat if their is some error while running a .ahk script but still i want to continue by skipping the error


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: November 16th, 2009, 11:57 am 
Offline

Joined: February 17th, 2008, 7:09 am
Posts: 536
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. :)


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: November 16th, 2009, 12:00 pm 
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???


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: November 16th, 2009, 12:11 pm 
Offline

Joined: February 17th, 2008, 7:09 am
Posts: 536
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 November 16th, 2009, 12:21 pm, edited 1 time in total.

Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: November 16th, 2009, 12:17 pm 
Offline

Joined: February 17th, 2008, 7:09 am
Posts: 536
rtcvb32 wrote:
Here's what you do. READ THE DOCUMENTATION.


Maybe i was being to subtle...


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: November 16th, 2009, 12:19 pm 
thanks a lot man its a good idea will work on it


Report this post
Top
  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 8 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: Bing [Bot], Cerberus, Google [Bot], Google Feedfetcher, Maestr0, rbrtryn, Tipsy3000, XstatyK and 64 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group