1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
|
<?xml version="1.0" encoding="UTF-8"?>
<helpdocument version="1.0">
<!--
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
-->
<meta>
<topic id="text/sbasic/shared/ErrVBA">
<title id="tit" xml-lang="en-US">Err VBA Object</title>
<filename>/text/sbasic/shared/ErrVBA.xhp</filename>
</topic>
</meta>
<body>
<section id="VBAErrObject">
<bookmark branch="index" id="N0010">
<bookmark_value>Err object</bookmark_value>
<bookmark_value>Error;raising</bookmark_value>
<bookmark_value>Error;handling</bookmark_value>
</bookmark>
<h1 id="N0011"><variable id="ErrVBAh1"><link href="text/sbasic/shared/ErrVBA.xhp" name="Err object [VBA]">Err Object [VBA]</link></variable></h1>
<paragraph role="paragraph" id="N0012">Use VBA <literal>Err</literal> object to raise or handle runtime errors.</paragraph>
</section>
<paragraph role="paragraph" id="N0012b"><literal>Err</literal> is a built-in VBA global object that allows:</paragraph>
<list type="unordered">
<listitem><paragraph role="paragraph" id="N0013">to raise predefined Basic errors</paragraph></listitem>
<listitem><paragraph role="paragraph" id="N0014">to throw user-defined exceptions</paragraph></listitem>
<listitem><paragraph role="paragraph" id="N0015">to name the routine originating the error</paragraph></listitem>
<listitem><paragraph role="paragraph" id="N0016">to describe the error and possible solutions</paragraph></listitem>
</list>
<embed href="text/sbasic/shared/00000003.xhp#vbasupport"/>
<paragraph role="paragraph" id="N0017">VBA <literal>Err</literal> object has the following properties and methods:</paragraph>
<h2 id="N0018">Properties</h2>
<bascode>
<paragraph role="bascode" id="N0019" localize="false">Err.Description As String</paragraph>
</bascode>
<paragraph role="paragraph" id="N0020"><emph>Description</emph> property gives the nature of the error. It details the various reasons that may cause the error. Ideally, it provides the multiple course of actions to help solve the issue and prevent its reoccurrence. Its alias is Basic <link href="text/sbasic/shared/03050300.xhp" name ="Error function">Error</link> function for %PRODUCTNAME predefined errors.</paragraph>
<bascode>
<paragraph role="bascode" id="N0021" localize="false">Err.Number As Long</paragraph>
</bascode>
<paragraph role="paragraph" id="N0022">This the error code associated with the error. <literal>Err</literal> object default property is <emph>Number</emph>. Its alias is %PRODUCTNAME Basic <link href="text/sbasic/shared/03050200.xhp" name ="Err">Err</link> function.</paragraph>
<bascode>
<paragraph role="bascode" id="N0023" localize="false">Err.Source As String</paragraph>
</bascode>
<paragraph role="paragraph" id="N0024"><emph>Source</emph> indicates the name of the routine that produces the error. <emph>Source</emph> is an option for user-defined errors.</paragraph>
<h2 id="N0025">Methods</h2>
<bascode>
<paragraph role="bascode" id="N0026" localize="false">Err.Clear()</paragraph>
</bascode>
<paragraph role="paragraph" id="N0027">Resets description, <link href="text/sbasic/shared/03050100.xhp" name ="error line">Erl</link>, number and source properties of current error. Its alias is %PRODUCTNAME Basic <link href="text/sbasic/shared/03050500.xhp" name ="Resume statement">Resume</link> statement.</paragraph>
<bascode>
<paragraph role="bascode" id="N0028" localize="false">Err.Raise(Number As Long, Optional source As String, Optional description As String)</paragraph>
</bascode>
<paragraph role="paragraph" id="N0029">Throws user-defined errors or predefined errors. Its alias is %PRODUCTNAME Basic <link href="text/sbasic/shared/03050200.xhp" name ="Error statement">Error</link> statement.</paragraph>
<h3 id="N0030">Parameters</h3>
<paragraph role="paragraph" id="N0031"><emph>Number</emph> A user-defined or predefined error code to be raised.</paragraph>
<note id="N0032">Error code range 0-2000 is reserved for %PRODUCTNAME Basic. User-defined errors may start from higher values in order to prevent collision with %PRODUCTNAME Basic future developments.</note>
<paragraph role="paragraph" id="N0033"><emph>Source</emph> The name of the routine raising the error. A name in the form of "myLibrary.myModule.myProc" is recommended.</paragraph>
<paragraph role="paragraph" id="N0034"><emph>Description</emph> A description of the problem leading to stop the running process, accompanied with the various reasons that may cause it. A detailed list of the possible course of actions that may help solve the problem is recommended.</paragraph>
<embed href="text/sbasic/shared/00000003.xhp#functexample"/>
<bascode>
<paragraph role="bascode" id="N0036" localize="false">Option VBASupport 1</paragraph>
<paragraph role="bascode" id="N0037" localize="false"> </paragraph>
<paragraph role="bascode" id="N0038" localize="false">Sub ThrowErrors</paragraph>
<paragraph role="bascode" id="N0039" localize="false"> Dim aDesc As String : aDesc = Space(80)</paragraph>
<paragraph role="bascode" id="N003A" localize="false"> On Local Error GoTo AlertAndExecNext</paragraph>
<paragraph role="bascode" id="N0040" localize="false"> Err.Raise(91, "ThrowErrors", Error(91))</paragraph>
<paragraph role="bascode" id="N0041" xml-lang="en-US"> Err.Raise 2020, Description:="This is an intented user-defined error …"</paragraph>
<paragraph role="bascode" id="N0042" localize="false"> Err.Raise(4096, "Standard.Module1.ThrowErrors", aDesc)</paragraph>
<paragraph role="bascode" id="N0043" localize="false"> Exit Sub</paragraph>
<paragraph role="bascode" id="N0044" localize="false">AlertAndExecNext:</paragraph>
<paragraph role="bascode" id="N0045" xml-lang="en-US"> errTitle = "Error "& Err &" at line "& Erl &" in "& Err.Source</paragraph>
<paragraph role="bascode" id="N0046" localize="false"> MsgBox Err.Description, MB_ICONEXCLAMATION, errTitle</paragraph>
<paragraph role="bascode" id="N0047" localize="false"> Resume Next</paragraph>
<paragraph role="bascode" id="N0048" localize="false">End Sub</paragraph>
</bascode>
<h2 id="N0050">Exception ClassModule</h2>
<tip id="N0049">A short <link href="text/sbasic/shared/classmodule.xhp" name="ClassModule option">ClassModule</link>, that wraps VBA <literal>Err</literal> object, can distribute <literal>Err</literal> properties and methods for standard %PRODUCTNAME Basic modules.</tip>
<bascode>
<paragraph role="bascode" id="N0051" localize="false">Option ClassModule</paragraph>
<paragraph role="bascode" id="N0052" localize="false">Option VBASupport 1</paragraph>
<paragraph role="bascode" id="N0053" localize="false"> </paragraph>
<paragraph role="bascode" id="N0054" localize="false">Public Property Get Description As String</paragraph>
<paragraph role="bascode" id="N0055" localize="false"> Description = Err.Description</paragraph>
<paragraph role="bascode" id="N0056" localize="false">End Property</paragraph>
<paragraph role="bascode" id="N0057" localize="false">Public Property Get Number As Long</paragraph>
<paragraph role="bascode" id="N0058" localize="false"> Number = Err.Number</paragraph>
<paragraph role="bascode" id="N0059" localize="false">End Property</paragraph>
<paragraph role="bascode" id="N0060" localize="false">Public Property Get Source As String</paragraph>
<paragraph role="bascode" id="N0061" localize="false"> Source = Err.Source</paragraph>
<paragraph role="bascode" id="N0062" localize="false">End Property</paragraph>
<paragraph role="bascode" id="N0063" localize="false">Public Sub Clear</paragraph>
<paragraph role="bascode" id="N0064" localize="false"> Err.Clear</paragraph>
<paragraph role="bascode" id="N0065" localize="false">End Sub</paragraph>
<paragraph role="bascode" id="N0066" localize="false">Public Sub Raise( number As Long, Optional Source As String, Optional Description As String)</paragraph>
<paragraph role="bascode" id="N0067" localize="false"> Err.Raise number, Source, Description</paragraph>
<paragraph role="bascode" id="N0068" localize="false">End Sub</paragraph>
</bascode>
<h2 id="N0069">Example</h2>
<bascode>
<paragraph role="bascode" id="N0070" localize="false">Function Exc As Object</paragraph>
<paragraph role="bascode" id="N0071" localize="false"> Exc = New Exception</paragraph>
<paragraph role="bascode" id="N0072" localize="false">End Function</paragraph>
<paragraph role="bascode" id="N0073" localize="false"> </paragraph>
<paragraph role="bascode" id="N0074" localize="false">Sub aRoutine</paragraph>
<paragraph role="bascode" id="N0075" localize="false">try:</paragraph>
<paragraph role="bascode" id="N0076" localize="false"> On Local Error GoTo catch:</paragraph>
<paragraph role="bascode" id="N0077" localize="false"> Exc.Raise(4096, "myLib.myModule.aRoutine", _</paragraph>
<paragraph role="bascode" id="N0078" xml-lang="en-US"> "Any multi-line description for this user-defined exception")</paragraph>
<paragraph role="bascode" id="N0079" xml-lang="en-US"> ' your code goes here …</paragraph>
<paragraph role="bascode" id="N0080" localize="false">finally:</paragraph>
<paragraph role="bascode" id="N0081" localize="false"> Exit Sub</paragraph>
<paragraph role="bascode" id="N0082" localize="false">catch:</paragraph>
<paragraph role="bascode" id="N0083" xml-lang="en-US"> errTitle = "Error "& Exc.Number &" at line "& Erl &" in "& Exc.Source</paragraph>
<paragraph role="bascode" id="N0084" localize="false"> MsgBox Exc.Description, MB_ICONSTOP, errTitle</paragraph>
<paragraph role="bascode" id="N0085" localize="false"> Resume finally</paragraph>
<paragraph role="bascode" id="N0086" localize="false">End Sub</paragraph>
</bascode>
<note id="N0088">The <link href="text/sbasic/shared/03050300.xhp" name="Error statement">Error</link> statement or an Exception-like class module can be used interchangeably, while the latter adds extra features.</note>
<section id="relatedtopics">
<embed href="text/sbasic/shared/03050000.xhp#ErrHandlingh1"/>
<embed href="text/sbasic/shared/classmodule.xhp#classmodulestatement"/>
<embed href="text/sbasic/shared/03103350.xhp#vbasupportstatement"/>
</section>
</body>
</helpdocument>
|