This article focuses to explain about error handling in Microsoft Small Basic programming language.


No Keywords

Old BASIC languages have error handling functions such as ON ERROR GOTO statement. But Small Basic language doesn't have such keywords.

Error Handling

In Small Basic, File object has functions to handling errors.  Following sample shows two points of file error handling.  One is to get results of File operations.  This sample code gets a result of File.WriteContents operation in OnButtonClicked subroutine.  The other is to check File.LastError property.  This sample shows the last error if the result is "FAILED" in FileHelper_ShowError subroutine.

' Small Memo
' Version 0.1
' Copyright © 2019 Nonki Takahashi. The MIT License.
' Program ID HQM389
 
GraphicsWindow.Title = "Small Memo v0.1"
Init()
 
Sub Init
  size = 300
  GraphicsWindow.Width = size
  GraphicsWindow.Height = size
  GraphicsWindow.BackgroundColor = "#FFFF66"
  GraphicsWindow.BrushColor = "Black"
  GraphicsWindow.FontBold = "False"
  GraphicsWindow.FontName = "Arial"
  fs = 30
  GraphicsWindow.FontSize = fs
  Controls.AddButton("Save and Close", size - fs * 7.4, 0)
  tbox = Controls.AddMultiLineTextBox(0, fs * 1.5)
  Controls.SetSize(tbox, size, size - fs * 1.5)
  Shapes.SetOpacity(tbox, 60)
  path = Program.Directory + "\memo.txt"
  buf = File.ReadContents(path)
  Controls.SetTextBoxText(tbox, buf)
  Controls.ButtonClicked = OnButtonClicked
  op = ""
  dirs = ""
  files = ""
  indent = ""
EndSub
 
Sub OnButtonClicked
  buf = Controls.GetTextBoxText(tbox)
  result = File.WriteContents(path, buf)
  File_CheckResult()
  If result = "FAILED" Then
    TextWindow.Pause()
  EndIf
  Program.End()
EndSub
  
Sub File_CheckResult
  ' File | check File operation result
  ' param result, dirs or files - result of a File operation
  ' param op - name of File operation
  If (op = "GetDirectories") And (dirs = "FAILED") Then
    FileHelper_ShowError()
  ElseIf (op = "GetFiles") And (files = "FAILED") Then
    FileHelper_ShowError()
  ElseIf result = "FAILED" Then
    FileHelper_ShowError()
  EndIf
EndSub
 
Sub FileHelper_ShowError
  ' File Helper | show last errer
  ' param op - oparation name
  ' param indent - indent space if needed
  TextWindow.ForegroundColor = "Yellow"
  TextWindow.WriteLine(indent + op + ":FAILED")
  TextWindow.WriteLine(indent + "LastError:" + File.LastError)
  TextWindow.ForegroundColor = "Gray"
EndSub

Usually this sample doesn't show error.  But after changing read-only attribute of the output file as below, an error will be shown.

The error will be show like below.

This kind of error handling (or error logging) will be helpful to know what exactly happened in the program.

Errors Can' t Be Handled

There are a lot of other errors that can not be handled from Small Basic program such as syntax error, runtime error, and so on. See more detail about these errors here.


See Also

Other Resources