Tenesta ScriptForge.Exception

Tenesta Exception (unnatak) er ei samling av metodar som hjelper til med med feilsĂžking (debugging) av kode i Basic- og Python-skript og feilhandsaming i Basic-skript.

NÄr det oppstÄr ein kÞyretidsfeil i Basic-skript, hjelper metodane og eigenskapane til Exception-tenesta til Ä identifisere feilsamanhengen og tillata Ä handsama han.

tip

Feil og Ätvaringar som oppstÄr med tenesta Exception vert lagra i minnet og kan hentast fram ved hjelp av metoden Console.


Tenestekonsollen Exception lagrar hendingar, variabelverdiar og opplysningar om feil. Bruk konsollen nÄr BASIC-IDE ikkje er lett tilgjengeleg, for eksempel i Calc user defined functions eller under handsaming av hendingar.

Bruk metoden DebugPrint for Ă„ leggja kva relevant informasjon som helst til i konsollen. KonsolloppfĂžringar kan dumpast til ei tekstfil eller visualiserast i eit dialogvindauge.

NÄr det oppstÄr ein feil, kan ein programmakro:

  1. Rapporter feilen i konsollen Exception

  2. Informere brukaren om feilen ved hjelp av ei standardmelding eller ei eigendefinert melding

  3. Eventuelt stoppa utfĂžringa

I Python-skript vert tenesta Exception mest brukt til feilsÞkingsfÞremÄl. Metodar som DebugPrint, Console og DebugDisplay er nyttige for raskt Ä skriva ut meldingar, logge data og opna konsollvindauget frÄ eit Python-skript.

note

Ikkje alle metodar og eigenskapar er tilgjengelege for Python-skript sidan Python-sprÄket alt har eit omfattande unntakshandsamingssystem.


Oppkall av tenester

I Basic

Eksempla nedanfor viser tre forskjellige framgangsmÄtar for Ä kalle opp metoden Raise. Alle andre metodar kan utfÞrast pÄ ein liknande mÄte.


    SF_Exception.Raise(...)
  

    Dim exc : exc = SF_Exception
    exc.Raise(...)
  

    Dim exc : exc = CreateScriptService("Exception")
    exc.Raise(...)
  
I Python

Kodesnutten nedanfor lagar eit eksemplar av tenesta Exception, loggar ei melding og viser vindauget Console.


    from scriptforge import CreateScriptService
    exc = CreateScriptService("Exception")
    someVar = 100
    exc.DebugPrint("Value of someVar", someVar)
    exc.Console()
  

Eigenskapar

Eigenskapane som er oppfĂžrt nedanfor er berre tilgjengelege for Basic-skript.

Namn

Skriveverna

Beskriving

Description

Nei

Teksten i feilmeldinga.

Standardverdien er "" eller ein streng som inneheld Basic-feilmeldinga om kĂžyretid.

Number

Nei

Koden for feilen. Det kan vera ein numerisk verdi eller ein tekst.

Standardverdien er 0 eller den talverdien som svarar til koden for Basic kĂžyretidsfeil.

Source

Nei

Staden i koden der feilen oppstod. Det kan vera ein talverdi eller ein tekst.

Standardverdien er 0 eller kodelinjenummeret for ein standard Basic kĂžyretidsfeil.


tip

NÄr du set eller ryddar ein Exception vert eigenskapane nullstilte.


note

Feilkodane 0-2000 er reserverte for LibreOffice Basic. Brukardefinerte feil mÄ byrja frÄ hÞgare verdiar for Ä unngÄ kollisjon med framtidige utgÄver av LibreOffice Basic.


Liste over metodar i tenesta Exception

Clear
Console
ConsoleClear

ConsoleToFile
DebugDisplay
DebugPrint

PythonPrint
PythonShell
Raise
RaiseWarning


Clear

Tilbakestiller gjeldande feilstatus og slettar SF_Exception-eigenskapane.

note

Denne metoden er tilgjengeleg berre for Basic-skript.


Syntaks:


    SF_Exception.Clear()
  

Eksempel:

Eksempelet nedanfor viser korleis du fangar opp eit unnatak for deling med null, der feilkoden er 11.


    Sub Example_Clear()
        Dim a, b, c
        On Local Error GoTo Catch
        Try:
            a = 10 : b = 0
            c = a / b
            '...
            Exit Sub
        Catch:
            If SF_Exception.Number = 11 Then SF_Exception.Clear()
            ' viss divisjon med null, ignorer feilen
    End Sub
  
tip

Viss du vil ha ei fullstendig liste over feilkoder for Basic kÞyretid, kan du sjÄ Debugging a Basic Program.


Console

Viser konsollmeldingane i eit modalt eller ikkje-modalt dialogvindauge. I begge modusane vert alle tidlegare meldingar utsend av ein DebugPrint()-metode viste, eller som er eit resultat av eit unnatak. I ikkje-modal modus vert dei etterfĂžlgjande oppfĂžringane lagde til automatisk.

Viss konsollen alt er ope som ikkje-modal, vert han flytt fremst.

Ein modal konsoll kan berre lukkast av brukaren. Ein ikkje-modal konsoll kan anten lukkast av brukaren eller ved makroavslutning.

Syntaks:

exc.Console(modal: bool = True)

Parametrar:

modal: Bestemmer om konsollvindauget er modalt (Sann) eller ikkje-modalt (Usann). Standardverdien er Sann

Eksempel:

I Basic

        SF_Exception.Console(Modal := False)
  
I Python

    exc.Console(modal = False)
  

ConsoleClear

TÞmmer konsollen og beheld ei valfri mengd av dei siste meldingane. Viss konsollen er slÄtt pÄ i ikkje-modal modus, vert han oppdatert.

Syntaks:

exc.ConsoleClear(keep: int = 0)

Parametrar:

keep: Talet pÄ nylege meldingar som skal behaldast. Standardverdien er 0

Eksempel:

Det neste eksempelet tĂžmmer konsollen men beheld dei 10 siste meldingane.

I Basic

        SF_Exception.ConsoleClear(10)
  
I Python

    exc.ConsoleClear(10)
  

ConsoleToFile

Eksporterer innhaldet i konsollen til ei tekstfil. Viss fila finst frÄ fÞr og konsollen ikkje er tom, vert ho overskrive utan varsel. Returnerer Sann viss vellukka.

Syntaks:

exc.ConsoleToFile(filename: str): bool

Parametrar:

filename: The name of the text file the console should be dumped into. The name is expressed according to the current FileNaming property of the SF_FileSystem service. By default, URL notation and the native operating system's format are both admitted.

Eksempel:

I Basic

        SF_Exception.ConsoleToFile("C:\Documents\myFile.txt")
  
I Python

    exc.ConsoleToFile(r"C:\Documents\myFile.txt")
  

DebugDisplay

Concatenates all the arguments into a single human-readable string and displays it in a MsgBox with an Information icon and an OK button.

The final string is also added to the Console.

Syntaks:

exc.DebugDisplay(arg0: any, [arg1: any, ...])

Parametrar:

arg0[, arg1, ...]: Any number of arguments of any type.

Eksempel:

I Basic

    SF_Exception.DebugDisplay("Current Value", someVar)
  
I Python

    exc.DebugDisplay("Current Value", someVar)
  

DebugPrint

Assembles all the given arguments into a single human-readable string and adds it as a new entry in the console.

Syntaks:

exc.DebugPrint(arg0: any, [arg1: any, ...])

Parametrar:

arg0[, arg1, ...]: Any number of arguments of any type.

Eksempel:

I Basic

    SF_Exception.DebugPrint(Null, Array(1, 2, 3), "line1" & Chr(10) & "Line2", DateSerial(2020, 04, 09))
    ' [NULL]   [ARRAY] (0:2) (1, 2, 3)  line1\nLine2  2020-04-09
  
I Python

    exc.DebugPrint(None, [1, 2, 3], "line1\nline2")
    # None  [1, 2, 3]  line1\nline2
  

PythonPrint

Displays the list of arguments in a readable form in the Python shell (APSO) console. Arguments are separated by a TAB character (simulated by spaces).

The same string is added to the ScriptForge debug console.

note

Denne metoden er tilgjengeleg berre for Basic-skript.


Syntaks:


  exc.PythonPrint(arg0: any, [arg1: any, ...])
  
warning

This method requires the installation of the APSO (Alternative Script Organizer for Python) extension. In turn APSO requires the presence of LibreOffice Python scripting framework. If APSO or Python are missing, an error occurs.


Parametrar:

arg0[, arg1, ...]: Any number of arguments of any type. The maximum length of each individual argument is 1024 characters.

Eksempel:


    exc.PythonPrint(a, Array(1, 2, 3), , "line1" & Chr(10) & "Line2", DateSerial(2020, 04, 09))
  
note

In python use simply the builtin print() statement.


PythonShell

Opens an APSO Python shell as a non-modal window. The Python script keeps running after the shell is opened. The output from print statements inside the script are shown in the shell.

Only a single instance of the APSO Python shell can be opened at any time. Hence, if a Python shell is already open, then calling this method will have no effect.

warning

This method requires the installation of the APSO (Alternative Script Organizer for Python) extension. In turn APSO requires the presence of LibreOffice Python scripting framework. If APSO or Python are missing, an error occurs.


Syntaks:

exc.PythonShell(variables: dict)

Parametrar:

variables: a Python dictionary with variable names and values that will be passed on to the APSO Python shell. By default all local variables are passed using Python's builtin locals() function.

Eksempel:

The example below opens the APSO Python shell passing all global and local variables considering the context where the script is running.


    exc.PythonShell({**globals(), **locals()})
  

When the APSO Python shell is open, any subsequent output printed by the script will be shown in the shell. Hence, the string printed in the example below will be displayed in the Python shell.


    exc.PythonShell()
    print("Hello world!")
  

Raise

Generates a run-time error. An error message is displayed to the user and reported in the console. The execution is stopped. The Raise() method can be placed inside the normal script flow or in a dedicated error-handling routine.

note

Denne metoden er tilgjengeleg berre for Basic-skript.


Syntaks:


    SF_Exception.Raise([Number As Variant], [Source As Variant], [Description As String])
  

The code snippets presented next are equivalent. They show alternative ways to raise an exception with code 2100.


    SF_Exception.Raise(2100)
  

    SF_Exception.Number = 2100
    SF_Exception.Raise()
  

    SF_Exception.Raise Number := 2100
  

Parametrar:

Number: The error code, as a number or as a string. Default value is that of Err Basic builtin function.

Source: The location of the error, as a number or as a string. Default value is that of Erl Basic builtin function.

Description: Meldinga som skal visast til brukaren og rapporterast i konsollen). Standardverdien er den same som i den innebyggde Basic-funksjonen Error$ (Feil$).

Eksempel:


    Sub Example_Raise()
        Dim a, b, c
        On Local Error GoTo Catch
        Try:
            a = 10 : b = 0
            c = a / b
            '...
            Exit Sub
        Catch:
            'See variants below ...
    End Sub
  

To raise an exception with the standard values:


    Catch:
        SF_Exception.Raise()
  

To raise an exception with a specific code:


    Catch:
        SF_Exception.Raise(11)
  

To replace the usual message:


    Catch:
        SF_Exception.Raise(, , "It is not a good idea to divide by zero.")
  

To raise an application error:


    Catch:
        SF_Exception.Raise("MyAppError", "Example_Raise()", "Something wrong happened !")
  

RaiseWarning

This method has exactly the same syntax, arguments and behavior as the Raise() method.

However, when a warning is raised, the macro execution is not stopped.

note

Denne metoden er tilgjengeleg berre for Basic-skript.


Syntaks:


    SF_Exception.RaiseWarning([Number As Variant], [Source As Variant], [Description As String])
  

Parametrar:

Number: The error code, as a number or as a string. Default value is that of Err Basic builtin function.

Source: The location of the error, as a number or as a string. Default value is that of Erl Basic builtin function.

Description: Meldinga som skal visast til brukaren og rapporterast i konsollen). Standardverdien er den same som i den innebyggde Basic-funksjonen Error$ (Feil$).

Eksempel:


    SF_Exception.RaiseWarning(Source:="Example_Raise()", _
        Description:="Something wrong happened !", _
        Number:="MyAppError")