Jump to content
Sign in to follow this  
Solarlight27

Catching Errors

Recommended Posts

Solarlight27

Is there a way to catch errors in AutoIt? SImilar to java's "Try-Catch" methods?

My program keeps on throwing errors and I need them to just go away, my program works fine, other than these errors.

Here's my code:

#NoTrayIcon
#include 
#include 
#include 
#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Res_requestedExecutionLevel=asInvoker
#Obfuscator_Parameters=/cs /cf
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****

;Opt("TrayMenuMode",1)
;$menu = TrayCreateItem("DriveLocker")
;$menu2 = TrayCreateItem("")
;$menuLock = TrayCreateItem("Lock Drive")
;TraySetState()

HotKeySet("{F9}", "_LockDrive")
HotKeySet("{PAUSE}", "_Switch")

$running = True
While 1
While ($running)
$letter = DriveGetDrive("REMOVABLE")
If IsArray($letter) Then
For $i = 1 to $letter[0]
_Check($letter[$i])
Next
EndIf
$time = TimerInit()
While TimerDiff($time) < 10000
If IsArray($letter) Then
For $i = 1 to $letter[0]
_Check($letter[$i])
Next
EndIf
Sleep(100)
WEnd
WEnd
Sleep(10*1000)
WEnd

Func _Switch()
If ($running) Then
$running = False
Else
$running = True
EndIf
EndFunc

Func _Check($strDriveLetter)
If ($running) Then
If FileExists($strDriveLetter & "\AutoIt3\Locker\IAmLocked") Then
_Unlock($strDriveLetter)
ElseIf FileExists($strDriveLetter & "\lock.txt") Then
_Unlock($strDriveLetter)
EndIf
EndIf
EndFunc

Func _Unlock($strDriveLetter)
If ($running) Then
$strPassword = InputBox("Security Check", "Enter your password.", "", "*", "", "", @DesktopWidth/2, @DesktopHeight/2,8, "passwordBox")
ToolTip("Enter Password NOW.")
_MouseTrap(@DesktopWidth, @DesktopHeight-100)
If ($strPassword = "PASSWORD") Then
_MouseTrap()
ToolTip("")
FileDelete($strDriveLetter & "\AutoIt3\Locker\IAmLocked")
FileDelete($strDriveLetter & "\lock.txt")
_ReAnimate($strDriveLetter)
ElseIf ($strPassword = "PASSWORDFAILSAFE") Then
_MouseTrap()
ToolTip("")
FileDelete($strDriveLetter & "\AutoIt3\Locker\IAmLocked")
FileDelete($strDriveLetter & "\lock.txt")
_ReAnimate($strDriveLetter)
Else
EjectVolume($strDriveLetter)
ToolTip("")
_MouseTrap()
EndIf
EndIf
EndFunc

Func _ReAnimate($strDriveLetter)
;Run(StringUpper($letter[$i]) & "\")
;Run(StringUpper($letter[$i]) & "\new.txt.lnk")
;Run(StringUpper($letter[$i]) & "\you.lnk")
;WinClose("AutoPlay")
;ProcessClose("mcagent.exe")
EndFunc

Func _LockDrive()
$letter = DriveGetDrive("REMOVABLE")
If IsArray($letter) Then
For $i = 1 To $letter[0]
If MsgBox(36,"Lock This Drive?","Do you wish to lock the " & StringUpper($letter[$i]) & " drive?") = 6 Then
FileWrite($letter[$i] & "\AutoIt3\Locker\IAmLocked","Locked")
_FileCreate($letter[$i] & "\lock.txt")
;EjectVolume($letter[$i])
EndIf
Next
EndIf
EndFunc

Share this post


Link to post
Share on other sites
BrewManNH

Is there a way to catch errors in AutoIt? SImilar to java's "Try-Catch" methods?

My program keeps on throwing errors and I need them to just go away, my program works fine, other than these errors.

A program that keeps erroring isn't working fine, just an FYI. What are the errors you're getting, and how are you trying to prevent them? Catching them isn't going to make them go away, you're just going to hide them (make them not crash the script), which means that your script still won't work right but no one would know it.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites
Solarlight27

Yes I do know that an erring program isn't working fine, I'm just hoping that a Band-aid solution can fix my problem here...

My script had been working fine for a long while, but then lately with my new flash drive it has been throwing errors, it still does what its programmed to do just it think at some there should still be a flash drive plugged in after its ejected. And the error, is a Windows error window that says "No Disk"

Edited by Solarlight27

Share this post


Link to post
Share on other sites
guinness

There are examples in the examples section by JScript & MrCreatoR which might be of interest. Just do a quick search.


UDF List:

 
_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_ArrayFilter/_ArrayReduce_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 22/04/2018

Share this post


Link to post
Share on other sites
Solarlight27

Sweet, found it, I still have to figure out his program and everything...

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Similar Content

    • tremolux66
      By tremolux66
      Is there a way to have Aut2Exe write its error messages to the console (stdout/stderr) instead of popping up a message window?
      We build our project software using Bash scripts that compile AutoIt scripts, compile C programs, install data files, etc. If Aut2Exe encounters an error when compiling an AutoIt script it pops-up a message window, which is problematic when running from a Bash script. It's worse when part or all of the build is executed remotely via SSH: if you're watching the build, you can kill the local script if it gets stuck (i.e., is waiting for Aut2Exe to exit), but the Aut2Exe process is still running on the remote machine and has to be killed there.
      A local Aut2Exe already exits with a non-zero status once the window is closed (which is only a minor problem; see below). The big snag is the remote processing; cleaner local processing would be a plus.
      Note: For use in repetitive compilation testing, I created an AutoIt script that waits for Aut2Exe error windows to appear and closes them. In a Bash test-script, this error monitoring script is started in the background at the beginning of the test and killed at the end. This could be used with local builds, but I doubt it can be adapted to work on a remote machine since the remote SSH processes run in Windows Session 0 (including the login shell). Session 0 doesn't seem to be an issue if Aut2Exe exits normally, however.
      Any suggestions?
      (Abandoning remote compilation is not an option at the moment; we're trying to work our way out of that.)
    • xtrim
      By xtrim
      Hi,

      I wrote a small script that displays a web page within autoit GUI.
      Is there a way to "catch" the clicks on the links on that web page?

      The web page is customizable, so I can define the link in any way I want...

      Tnx
×