| Server IP : 121.121.20.254 / Your IP : 216.73.216.202 Web Server : Microsoft-IIS/10.0 System : Windows NT WEB-SERVER 10.0 build 20348 (Windows Server 2022) AMD64 User : IUSR ( 0) PHP Version : 8.3.28 Disable Function : NONE MySQL : ON | cURL : ON | WGET : OFF | Perl : OFF | Python : OFF | Sudo : OFF | Pkexec : OFF Directory : /Program Files/LibreOffice/help/en-US/text/sbasic/shared/ |
Upload File : |
<!DOCTYPE html>
<html lang="en-US" dir="ltr">
<head>
<base href="../../../../">
<noscript><meta http-equiv="refresh" content="0; URL=../../../../en-US/noscript.html"></noscript>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Err VBA Object</title>
<link rel="shortcut icon" href="media/navigation/favicon.ico">
<link type="text/css" href="normalize.css" rel="Stylesheet">
<link type="text/css" href="prism.css" rel="Stylesheet">
<link type="text/css" href="default.css" rel="Stylesheet">
<script type="text/javascript" src="polyfills.js"></script><script type="text/javascript" src="languages.js"></script><script type="text/javascript" src="en-US/langnames.js"></script><script type="text/javascript" src="flexsearch.debug.js"></script><script type="text/javascript" src="prism.js"></script><script type="text/javascript" src="help2.js" defer></script><script type="text/javascript" src="a11y-toggle.js" defer></script><script type="text/javascript" src="paginathing.js" defer></script><script type="text/javascript" src="en-US/bookmarks.js" defer></script><script type="text/javascript" src="en-US/contents.js" defer></script><script type="text/javascript" src="help.js" defer></script><meta name="viewport" content="width=device-width,initial-scale=1">
</head>
<body>
<header id="TopLeftHeader"><a class="symbol" href="en-US/text/shared/05/new_help.html"><div></div></a><a class="logo" href="en-US/text/shared/05/new_help.html"><p dir="auto">LibreOffice 24.2 Help</p></a><div class="dropdowns"><div class="modules">
<button type="button" data-a11y-toggle="modules-nav" id="modules" aria-haspopup="true" aria-expanded="false" aria-controls="modules-nav">Module</button><nav id="modules-nav" hidden=""></nav>
</div></div></header><aside class="leftside"><input id="accordion-1" name="accordion-menu" type="checkbox"><label for="accordion-1" dir="auto">Contents</label><div id="Contents" class="contents-treeview"></div></aside><div id="SearchFrame"><div id="Bookmarks">
<input id="search-bar" type="search" class="search" placeholder="Search in bookmarks for chosen module" dir="auto"><div class="nav-container" tabindex="0"><nav class="index" dir="auto"></nav></div>
</div></div>
<div id="DisplayArea" itemprop="softwareHelp" itemscope="true" itemtype="http://schema.org/SoftwareApplication">
<a name="VBAErrObject"></a>
<a name="N0010"></a>
<meta itemprop="keywords" content="Err object">
<meta itemprop="keywords" content="Error,raising">
<meta itemprop="keywords" content="Error,handling">
<h1 id="N0011" dir="auto">
<a name="ErrVBAh1"></a>Err Object [VBA]</h1>
<p id="N0012" class="paragraph" dir="auto">Use VBA <span class="literal">Err</span> object to raise or handle runtime errors.</p>
<p id="N0012b" class="paragraph" dir="auto"><span class="literal">Err</span> is a built-in VBA global object that allows:</p>
<ul itemprop="Unordered" itemscope="true" itemtype="http://schema.org/ItemList" dir="auto">
<li itemprop="itemListElement" itemscope="true" itemtype="http://schema.org/ItemListUnordered" dir="auto"><p id="N0013" class="paragraph" dir="auto">to raise predefined Basic errors</p></li>
<li itemprop="itemListElement" itemscope="true" itemtype="http://schema.org/ItemListUnordered" dir="auto"><p id="N0014" class="paragraph" dir="auto">to throw user-defined exceptions</p></li>
<li itemprop="itemListElement" itemscope="true" itemtype="http://schema.org/ItemListUnordered" dir="auto"><p id="N0015" class="paragraph" dir="auto">to name the routine originating the error</p></li>
<li itemprop="itemListElement" itemscope="true" itemtype="http://schema.org/ItemListUnordered" dir="auto"><p id="N0016" class="paragraph" dir="auto">to describe the error and possible solutions</p></li>
</ul>
<div class="embedded">
<a name="vbasupport"></a>
<div class="warning">
<div class="noteicon" dir="auto"><img src="media/icon-themes/res/helpimg/warning.svg" alt="warning" style="width:40px;height:40px;"></div>
<div class="notetext"><p id="par_id051920171018124524" dir="auto">This constant, function or object is enabled with the statement <a target="_top" href="en-US/text/sbasic/shared/03103350.html"><span class="literal">Option VBASupport 1</span></a> placed before the executable program code in a module.</p></div>
</div>
<br>
</div>
<p id="N0017" class="paragraph" dir="auto">The VBA <span class="literal">Err</span> object has the following properties and methods:</p>
<h2 id="N0018" dir="auto">Properties</h2>
<div class="bascode" itemscope="true" itemtype="http://schema.org/SoftwareSourceCode" itemprop="codeSampleType" content="snippet" data-tooltip="Click on text to copy to clipboard"><pre dir="auto"><code class="language-visual-basic line-numbers">
Err.Description As String
</code></pre></div>
<p id="N0020" class="paragraph" dir="auto">The <span class="emph">Description</span> property gives the nature of the error. <span class="emph">Description</span> details the various reasons that may be the cause of the error. Ideally, it provides the multiple course of actions to help solve the issue and prevent its reoccurrence. The Basic alias is the <a target="_top" href="en-US/text/sbasic/shared/03050300.html">Error</a> function for LibreOffice predefined errors.</p>
<div class="bascode" itemscope="true" itemtype="http://schema.org/SoftwareSourceCode" itemprop="codeSampleType" content="snippet" data-tooltip="Click on text to copy to clipboard"><pre dir="auto"><code class="language-visual-basic line-numbers">
Err.Number As Long
</code></pre></div>
<p id="N0022" class="paragraph" dir="auto">The error code associated with the error. <span class="literal">Err</span> object default property is <span class="emph">Number</span>. The LibreOffice Basic alias is the <a target="_top" href="en-US/text/sbasic/shared/03050200.html">Err</a> function.</p>
<div class="bascode" itemscope="true" itemtype="http://schema.org/SoftwareSourceCode" itemprop="codeSampleType" content="snippet" data-tooltip="Click on text to copy to clipboard"><pre dir="auto"><code class="language-visual-basic line-numbers">
Err.Source As String
</code></pre></div>
<p id="N0024" class="paragraph" dir="auto"><span class="emph">Source</span> indicates the name of the routine that produces the error. <span class="emph">Source</span> is an option for user-defined errors.</p>
<h2 id="N0025" dir="auto">Methods</h2>
<div class="bascode" itemscope="true" itemtype="http://schema.org/SoftwareSourceCode" itemprop="codeSampleType" content="snippet" data-tooltip="Click on text to copy to clipboard"><pre dir="auto"><code class="language-visual-basic line-numbers">
Err.Clear()
</code></pre></div>
<p id="N0027" class="paragraph" dir="auto">Resets description, <a target="_top" href="en-US/text/sbasic/shared/03050100.html">Erl</a>, number and source properties of current error. The LibreOffice Basic alias is the <a target="_top" href="en-US/text/sbasic/shared/03050500.html">Resume</a> statement.</p>
<div class="bascode" itemscope="true" itemtype="http://schema.org/SoftwareSourceCode" itemprop="codeSampleType" content="snippet" data-tooltip="Click on text to copy to clipboard"><pre dir="auto"><code class="language-visual-basic line-numbers">
Err.Raise(Number As Long, Optional source As String, Optional description As String)
</code></pre></div>
<p id="N0029" class="paragraph" dir="auto">Throws user-defined errors or predefined errors. The LibreOffice Basic alias is the <a target="_top" href="en-US/text/sbasic/shared/03050200.html">Error</a> statement.</p>
<h3 id="N0030" dir="auto">Parameters</h3>
<p id="N0031" class="paragraph" dir="auto"><span class="emph">Number</span>: A user-defined or predefined error code to be raised.</p>
<a name="RestrictedErrorCodes"></a>
<div class="note">
<div class="noteicon" dir="auto"><img src="media/icon-themes/res/helpimg/note.svg" alt="note" style="width:40px;height:40px;"></div>
<div class="notetext"><p id="N0032" dir="auto">Error code range 0-2000 is reserved for LibreOffice Basic. User-defined errors may start from higher values in order to prevent collision with LibreOffice Basic future developments.</p></div>
</div>
<br>
<p id="N0033" class="paragraph" dir="auto"><span class="emph">Source</span>: The name of the routine raising the error. A name in the form of "myLibrary.myModule.myProc" is recommended.</p>
<p id="N0034" class="paragraph" dir="auto"><span class="emph">Description</span>: 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.</p>
<div class="embedded">
<a name="functexample"></a>
<h3 id="hd_id061420171139088233" dir="auto">Example:</h3>
</div>
<div class="bascode" itemscope="true" itemtype="http://schema.org/SoftwareSourceCode" itemprop="codeSampleType" content="snippet" data-tooltip="Click on text to copy to clipboard"><pre dir="auto"><code class="language-visual-basic line-numbers">
Option VBASupport 1
Sub ThrowErrors
Dim aDesc As String : aDesc = Space(80)
On Local Error GoTo AlertAndExecNext
Err.Raise(91, "ThrowErrors", Error(91))
Err.Raise 2020, Description:="This is an intended user-defined error …"
Err.Raise(4096, "Standard.Module1.ThrowErrors", aDesc)
Exit Sub
AlertAndExecNext:
errTitle = "Error "& Err &" at line "& Erl &" in "& Err.Source
MsgBox Err.Description, MB_ICONEXCLAMATION, errTitle
Resume Next
End Sub
</code></pre></div>
<h2 id="N0050" dir="auto">Exception ClassModule</h2>
<div class="tip">
<div class="noteicon" dir="auto"><img src="media/icon-themes/res/helpimg/tip.svg" alt="tip" style="width:40px;height:40px;"></div>
<div class="notetext"><p id="N0049" dir="auto">A short <a target="_top" href="en-US/text/sbasic/shared/classmodule.html">ClassModule</a>, that wraps VBA <span class="literal">Err</span> object, can distribute <span class="literal">Err</span> properties and methods for standard LibreOffice Basic modules.</p></div>
</div>
<br>
<div class="bascode" itemscope="true" itemtype="http://schema.org/SoftwareSourceCode" itemprop="codeSampleType" content="snippet" data-tooltip="Click on text to copy to clipboard"><pre dir="auto"><code class="language-visual-basic line-numbers">
Option ClassModule
Option VBASupport 1
Public Property Get Description As String
Description = Err.Description
End Property
Public Property Get Number As Long
Number = Err.Number
End Property
Public Property Get Source As String
Source = Err.Source
End Property
Public Sub Clear
Err.Clear
End Sub
Public Sub Raise( number As Long, Optional Source As String, Optional Description As String)
Err.Raise number, Source, Description
End Sub
</code></pre></div>
<h2 id="N0069" dir="auto">Example</h2>
<div class="bascode" itemscope="true" itemtype="http://schema.org/SoftwareSourceCode" itemprop="codeSampleType" content="snippet" data-tooltip="Click on text to copy to clipboard"><pre dir="auto"><code class="language-visual-basic line-numbers">
Function Exc As Object
Exc = New Exception
End Function
Sub aRoutine
try:
On Local Error GoTo catch:
Exc.Raise(4096, "myLib.myModule.aRoutine", _
"Any multi-line description for this user-defined exception")
' your code goes here …
finally:
Exit Sub
catch:
errTitle = "Error "& Exc.Number &" at line "& Erl &" in "& Exc.Source
MsgBox Exc.Description, MB_ICONSTOP, errTitle
Resume finally
End Sub
</code></pre></div>
<div class="note">
<div class="noteicon" dir="auto"><img src="media/icon-themes/res/helpimg/note.svg" alt="note" style="width:40px;height:40px;"></div>
<div class="notetext"><p id="N0088" dir="auto">The <a target="_top" href="en-US/text/sbasic/shared/03050300.html">Error</a> statement or an Exception-like class module can be used interchangeably, while the latter adds extra features.</p></div>
</div>
<br>
<a name="relatedtopics"></a><div class="relatedtopics">
<p class="related" itemprop="mentions" dir="auto"><a name="related"></a><span class="emph">Related Topics</span>
</p>
<div class="relatedbody" itemprop="mentions">
<div class="embedded"><p class="embedded" dir="auto"><a target="_top" href="en-US/text/sbasic/shared/03050000.html">Error-Handling Functions</a></p></div>
<div class="embedded"><p class="embedded" dir="auto"><a target="_top" href="en-US/text/sbasic/shared/classmodule.html">Option ClassModule Statement</a></p></div>
<div class="embedded"><p class="embedded" dir="auto"><a target="_top" href="en-US/text/sbasic/shared/03103350.html">Option VBASupport Statement</a></p></div>
<div class="embedded"><p class="embedded" dir="auto"><a target="_top" href="en-US/text/sbasic/shared/03/sf_exception.html"><span class="literal">ScriptForge</span>.<span class="literal">Exception</span> service</a></p></div>
</div>
</div>
</div>
<div id="DonationFrame"></div>
<footer><div id="DEBUG" class="debug">
<h3 class="bug">Help content debug info:</h3>
<p dir="auto">This page is: <a href="https://opengrok.libreoffice.org/xref/help/source/text/sbasic/shared/ErrVBA.xhp" target="_blank">/text/sbasic/shared/ErrVBA.xhp</a></p>
<p dir="auto">Title is: Err VBA Object</p>
<p id="bm_module" dir="auto"></p>
<p id="bm_system" dir="auto"></p>
<p id="bm_HID" dir="auto"></p>
</div></footer>
</body>
</html>