Vb Net Innerexception



This document was published with and applies to ArcGIS 9.3.
A 10 version also exists. A 9.2 version also exists.
  1. Vb.net Ex.innerexception
  2. Vb.net Ex.innerexception.message
  3. Vb.net Inner Exception Example
Summary The error handling construct in Visual Studio .NET is known as structured exception handling. The constructs used may be new to Visual Basic users but should be familiar to users of C++ or Java.

If the Exception.InnerException property of the current exception is a null reference (not set or empty), this property returns the current exception. Remarks A chain of exceptions consists of a set of exceptions such that each exception in the chain was thrown as a direct result of the exception referenced in its InnerException property. A few weeks ago, John Gruber posted Quit Confirmation for Safari on MacOS.I was inspired to make a few changes to Mr. Gruber's script and add a couple more. Check out my AppleScripts GitHub repo for quit confirmation on Safari and Chrome as well as a tab close confirmation on Safari. View VB.NET questions; View SQL questions. Almost every developer face that problem of having to walk the InnerException chain to find a specific piece of. If you read through this article, you'll notice that all the samples I used were in C#. That's because C# is my preferred language, and because VB.NET has a few guidelines of its own. Emulate C# 'using' statement. Unfortunately, VB.NET still doesn't have the using statement. Whidbey will have it, but until it's released, everytime you.

Structured exception handling implementation is straightforward, and the same concepts are applicable to VB.NET or C#. VB.NET allows backward compatibility by also providing unstructured exception handling via the familiar OnError GoTo statement and Err object, although this model is not discussed in this topic.

Using exceptions

Exceptions are used to handle error conditions in Visual Studio .NET and provide information about the error condition. An exception is an instance of a class that inherits from the System.Exception base class. Many different types of exceptions are provided by the .NET Framework, and it is also possible to create your own exceptions. Each type extends the basic functionality of the System.Exception by allowing further access to information about the specific type of error that has occurred. An instance of an exception is created and thrown when the .NET Framework encounters an error condition. You can handle exceptions by using the Try, Catch, and Finally construct.

Try, Catch, and Finally construct

This construct allows you to catch errors that are thrown within your code. An example of this construct is shown below. An attempt is made to rotate an envelope, which throws an error. See the following code example:

[C#]
[VB.NET]
Place a Try block around code that can fail. If the application throws an error within the Try block, the point of execution switches to the first Catch block. The Catch block handles a thrown error. The application executes the Catch block when the Type of a thrown error matches the error Type specified by the Catch block. You can have more than one Catch block to handle different kinds of errors. The following code example verifies if the exception thrown is a DivideByZeroException:

[C#]
[VB.NET]
If you do have more than one Catch block, the more specific exception Types should precede the general System.Exception, which will always succeed the type check.
The application always executes the Finally block after the Try block completes, or after a Catch block if an error was thrown. Therefore, the Finally block should contain code that must always be executed, for example, to clean up resources such as file handles or database connections.
If you do not have any cleanup code, you do not need to include a Finally block.

Code without exception handling

If a line of code that is not contained in a Try block throws an error, the .NET runtime searches for a Catch block in the calling function, continuing up the call stack until a Catch block is found.
If a Catch block is not specified in the call stack, the exact outcome can depend on the location of the executed code and the configuration of the .NET runtime. Therefore, it is advisable to at least include a Try, Catch, Finally construct for all entry points to a program.

Errors from COM components

The structured exception handling model differs from the HRESULT model used by the component object model (COM). C++ developers can easily ignore an error condition in an HRESULT. However, in Visual Basic 6, an error condition in an HRESULT populates the Err object and raises an error.
The .NET runtime handling of errors from COM components is similar to the way COM errors were handled at Visual Basic 6. If a .NET program calls a function in a COM component (through the COM interop services), and returns an error condition as the HRESULT, the HRESULT is used to populate an instance of the COMException. This is then thrown by the .NET runtime, where it can be handled in the usual way by using a Try, Catch, Finally block.
Therefore, it is advisable to enclose all code that can raise an error in a COM component within a Try block, with a corresponding Catch block to catch a COMException. The following code example is the first example rewritten to check for an error from a COM component:

[C#]
[VB.NET]
The COMException belongs to the System.Runtime.InteropServices namespace. It provides access to the value of the original HRESULT via the ErrorCode property, which you can test to determine the error condition that occurred.

Throwing errors and the exception hierarchy

If you are coding a user interface, you may want to correct the error condition in the code and try the call again. Alternatively, you may want to report the error to users to let them decide the course of action to take. You can make use of the message property of the exception to identify the problem.
However, if you are writing a function that is only called from other code, you may want to deal with an error by creating a specific error condition and propagating this error to the caller. You can do this using the Throw keyword.
To throw the existing error to the caller function, write your error handler using the Throw keyword. See the following code example:

[C#]
[VB.NET]
If you want to propagate a different or more specific error back to the caller, create a new instance of an exception, populate it appropriately, and throw this exception back to the caller. The following code example uses the ApplicationException constructor to set the message property:

[C#]
[VB.NET]
However, if you do this, the original exception is lost. To allow complete error information to be propagated, the exception includes the InnerException property. This property should be set to equal the caught exception before the new exception is thrown. This creates an error hierarchy. Again, the following code example uses the ApplicationException constructor to set the InnerException and message properties:

[C#]
[VB.NET]
In this way, the function that eventually deals with the error condition can access all the information about the cause of the condition and its context. If you throw an error, the application executes the current function's Finally clause before control is returned to the calling function.

Writing your error handler

The best approach to handling an error depends on what error is thrown and in what context. For more information, see Best Practices for Handling Exceptions on the MSDN Web site.

System.SystemException Class

Assembly: Mscorlib.dll
Namespace: System
Summary
Defines the base class for predefined exceptions in the System namespace.
C# Syntax:
[Serializable]
public class SystemException : Exception
Remarks This class is provided as a means to differentiate between exceptions defined by the system versus exceptions defined by applications.

SystemException does not provide information as to the cause of the Exception. In most scenarios, instances of this class should not be thrown. In cases where this class is instantiated, a human-readable message describing the error should be passed to the constructor .

SystemException is thrown by the common language runtime when errors occur that are nonfatal and recoverable by user programs. These errors result from failed runtime check (such as an array out-of-bound error), and can occur during the execution of any method. SystemException adds no new functionality to Exception.

SystemException uses the HRESULT COR_E_SYSTEM, that has the value 0x80131501.

For a list of initial property values for an instance of SystemException, see the SystemException.#ctor constructors.

For more information on exceptions defined by applications, see ApplicationException.

See also:
System Namespace | Exception | MSDN: handlingthrowingexceptions

System.SystemException Member List:

Public Constructors
ctor #1Overloaded:
.ctor()
Default constructor. This constructor is called by derived class constructors to initialize state in this type.
ctor #2Overloaded:
.ctor(string message)
Initializes a new instance of the SystemException class with a specified error message.
ctor #4Overloaded:
.ctor(string message, Exception innerException)
Initializes a new instance of the SystemException class with a specified error message and a reference to the inner exception that is the cause of this exception.
Public Properties
HelpLink
(inherited from System.Exception)
Read-write
See base class member description: System.Exception.HelpLink
Gets or sets a link to the help file associated with this exception.
InnerException
(inherited from System.Exception)
Read-only
See base class member description: System.Exception.InnerException
Gets the Exception instance that caused the current exception.
Message
(inherited from System.Exception)
Read-only
See base class member description: System.Exception.Message
Gets a message that describes the current exception.
Source
(inherited from System.Exception)
Read-write
See base class member description: System.Exception.Source
Gets or sets the name of the application or the object that causes the error.
StackTrace
(inherited from System.Exception)
Read-only
See base class member description: System.Exception.StackTrace
Gets a string representation of the frames on the call stack at the time the current exception was thrown.
TargetSite
(inherited from System.Exception)
Read-only
See base class member description: System.Exception.TargetSite
Gets the method that throws the current exception.
Public Methods
Equals
(inherited from System.Object)
See base class member description: System.Object.Equals
Derived from System.Object, the primary base class for all objects.
GetBaseException
(inherited from System.Exception)
See base class member description: System.Exception.GetBaseException
When overridden in a derived class, returns the Exception that is the root cause of one or more subsequent exceptions.
GetHashCode
(inherited from System.Object)
See base class member description: System.Object.GetHashCode
Derived from System.Object, the primary base class for all objects.
GetObjectData
(inherited from System.Exception)
See base class member description: System.Exception.GetObjectData
When overridden in a derived class, sets the SerializationInfo with information about the exception.
GetType
(inherited from System.Object)
See base class member description: System.Object.GetType
Derived from System.Object, the primary base class for all objects.
ToString
(inherited from System.Exception)
See base class member description: System.Exception.ToString
Creates and returns a string representation of the current exception.
Protected Constructors
ctor #3Overloaded:
.ctor(SerializationInfo info, StreamingContext context)
Initializes a new instance of the SystemException class with serialized data.
Protected Properties
HResult
(inherited from System.Exception)
Read-write
See base class member description: System.Exception.HResult
Gets or sets HRESULT, a coded numerical value that is assigned to a specific exception.
Protected Methods
Finalize
(inherited from System.Object)
See base class member description: System.Object.Finalize
Derived from System.Object, the primary base class for all objects.
MemberwiseClone
(inherited from System.Object)
See base class member description: System.Object.MemberwiseClone
Derived from System.Object, the primary base class for all objects.

Hierarchy:

  • System.SystemException

System.SystemException Member Details

Overloaded
Summary
Initializes a new instance of the SystemException class.

Default constructor. This constructor is called by derived class constructors to initialize state in this type.
C# Syntax:
public SystemException();
Remarks This constructor initializes the Exception.Message property of the new instance to a system-supplied message that describes the error, such as 'A system error has occurred.' This message takes into account the current system culture.

The following table shows the initial property values for an instance of SystemException.


Property Value
Exception.InnerException A null reference ( not set or empty is Visual Basic).
Exception.Message The localized error message string.

Return to top

Overloaded
Summary
Initializes a new instance of the SystemException class with a specified error message.
C# Syntax:
public SystemException(
stringmessage
);
Parameters:

message

The message that describes the error.
Remarks The content of the message parameter should be understandable to the user. The caller of this constructor is required to ensure that this string has been localized for the current system culture.

The following table shows the initial property values for an instance of SystemException.


Property Value
Exception.InnerException A null reference ( not set or empty is Visual Basic).
Exception.Message The error message string.

Return to top

Overloaded
Summary
Initializes a new instance of the SystemException class with serialized data.
C# Syntax:
protected SystemException(
SerializationInfoinfo,
StreamingContextcontext
);
Parameters:

info

The object that holds the serialized object data.

context

The contextual information about the source or destination.
Remarks
This constructor is called during deserialization to reconstitute the exception object transmitted over a stream. For more information, see the conceptual topic at MSDN: serialization.
See also:
MSDN: serialization

Return to top

Overloaded
Summary
Initializes a new instance of the SystemException class with a specified error message and a reference to the inner exception that is the cause of this exception.
C# Syntax:
public SystemException(
stringmessage,
ExceptioninnerException
);
Parameters:

message

The error message that explains the reason for the exception.

innerException

The exception that is the cause of the current exception. If the innerException parameter is not a null reference (not set or empty), the current exception is raised in a catch block that handles the inner exception.
Remarks An exception that is thrown as a direct result of a previous exception can include a reference to the previous exception in the Exception.InnerException property. The Exception.InnerException property returns the same value that is passed into the constructor, or a null reference (not set or empty) if the Exception.InnerException property does not supply the inner exception value to the constructor.

The following table shows the initial property values for an instance of SystemException.


Property Value
Exception.InnerException The inner exception reference.
Exception.Message The error message string.
See also:
Exception | MSDN: handlingthrowingexceptions

Return to top

Property: (read-write)
Inherited
See base class member description: System.Exception.HelpLink

Summary
Gets or sets a link to the help file associated with this exception.
C# Syntax:
public virtual string HelpLink {get; set;}
Remarks The return value, which represents a help file, is a URN or URL. For example, the HelpLink value could be:

'file:///C:/Applications/Bazzal/help.html#ErrorNum42'

Return to top

Property: (read-write)
Inherited
See base class member description: System.Exception.HResult

Summary
Gets or sets HRESULT, a coded numerical value that is assigned to a specific exception.
C# Syntax:
protected int HResult {get; set;}
Remarks
HRESULT is a 32-bit value, divided into three different fields: a severity code, a facility code, and an error code. The severity code indicates whether the return value represents information, warning, or error. The facility code identifies the area of the system responsible for the error. The error code is a unique number that is assigned to represent the exception. Each exception is mapped to a distinct HRESULT. When managed code throws an exception, the runtime passes the HRESULT to the COM client. When unmanaged code returns an error, the HRESULT is converted to an exception, which is then thrown by the runtime.

Return to top

Property: (read-only)
Inherited
See base class member description: System.Exception.InnerException

Vb Net Innerexception
Summary
Gets the Exception instance that caused the current exception.
C# Syntax:
public Exception InnerException {get;}
Remarks When an exception X is thrown as a direct result of a previous exception Y, the InnerException property of X should contain a reference to Y.

Use the InnerException property to obtain the set of exceptions that led to the current exception.

You can create a new exception that catches an earlier exception. The code that handles the second exception can make use of the additional information from the earlier exception to handle the error more appropriately.

Suppose that there is a function that reads a file and formats the data from that file. In this example, as the code tries to read the file, an IOException is thrown. The function catches the IOException and throws a FileNotFoundException. The IOException could be saved in the Exception.InnerException property of the FileNotFoundException, enabling the code that catches the FileNotFoundException to examine what causes the initial error.

The Exception.InnerException property, which holds a reference to the inner exception, is set upon initialization of the exception object.

Example The following example demonstrates throwing and catching an exception that references an inner exception.

This code has the following output:

In Main catch block. Caught: Error caused by trying ThrowInner. Inner Exception is MyAppException: ExceptExample inner exception at ExceptExample.ThrowInner() at ExceptExample.CatchInner()

Return to top

Property: (read-only)
Inherited
See base class member description: System.Exception.Message

Summary
Gets a message that describes the current exception.
C# Syntax:
public virtual string Message {get;}
Remarks The text of Message should completely describe the error and should, when possible, explain how to correct it. The value of the Message property is included in the information returned by Exception.ToString.

The Message property is set only when creating an Exception. If no message was supplied to the constructor for the current instance, the system supplies a default message that is formatted using the current system culture.


Notes to inheritors: The Message property is overridden in classes that require control over message content or format. Application code typically accesses this property when it needs to display information about an exception that has been caught.

The error message should be localized.

Return to top

Property: (read-write)
Inherited
See base class member description: System.Exception.Source

Summary
Gets or sets the name of the application or the object that causes the error.
C# Syntax:
public virtual string Source {get; set;}
Remarks
If Exception.Source is not set, the name of the assembly where the exception originated is returned.

Return to top

Property: (read-only)
Inherited
See base class member description: System.Exception.StackTrace

Summary
Gets a string representation of the frames on the call stack at the time the current exception was thrown.
C# Syntax:
public virtual string StackTrace {get;}
Remarks The execution stack keeps track of all the methods that are in execution at a given instant. A trace of the method calls is called a stack trace. The stack trace listing provides a means to follow the call sequence to the line number in the method where the exception occurs.

StackTrace may not report as many method calls as expected, due to code transformations, such as inlining, that occur during optimization.


Notes to inheritors: The StackTrace property is overridden in classes that require control over the stack trace content or format.

By default, the stack trace is captured immediately before an exception object is thrown. Use Environment.StackTrace to get stack trace information when no exception is being thrown.

See also:
Environment.StackTrace

Return to top

Property: (read-only)
Inherited
See base class member description: System.Exception.TargetSite

Summary
Gets the method that throws the current exception.
C# Syntax:
public MethodBase TargetSite {get;}
Remarks
If the method that throws this exception is not available and the stack trace is not a null reference (not set or empty), Exception.TargetSite obtains the method from the stack trace. If the stack trace is a null reference , Exception.TargetSite also returns a null reference.

Return to top

Method: (
objectobj
)
Inherited
See base class member description: System.Object.Equals
C# Syntax:
public virtual bool Equals(
objectobj
);

For more information on members inherited from System.Object click on the link above.

Return to top

Method: ()
Inherited
See base class member description: System.Object.Finalize
C# Syntax:
~SystemException();

For more information on members inherited from System.Object click on the link above.

Return to top

Method: ()
Inherited
See base class member description: System.Exception.GetBaseException

Summary
When overridden in a derived class, returns the Exception that is the root cause of one or more subsequent exceptions.
C# Syntax:
public virtual Exception GetBaseException();
Return Value:
The first exception thrown in a chain of exceptions. If the Exception.InnerException property of the current exception is a null reference (not set or empty), this property returns the current exception.
Remarks A chain of exceptions consists of a set of exceptions such that each exception in the chain was thrown as a direct result of the exception referenced in its InnerException property. For a given chain, there can be exactly one exception that is the root cause of all other exceptions in the chain. This exception is called the base exception and its InnerException property always contains a null reference.

For all exceptions in a chain of exceptions, the GetBaseException method must return the same object (the base exception).

Use the GetBaseException method when you want to find the root cause of an exception but do not need information about exceptions that may have occurred between the current exception and the first exception.


Notes to inheritors: The GetBaseException method is overridden in classes that require control over the exception content or format.

Return to top

Method: ()
Inherited
See base class member description: System.Object.GetHashCode
C# Syntax:
public virtual int GetHashCode();

For more information on members inherited from System.Object click on the link above.

Return to top

Method: (
SerializationInfoinfo,
StreamingContextcontext
)
Inherited
See base class member description: System.Exception.GetObjectData

Summary
When overridden in a derived class, sets the SerializationInfo with information about the exception.
C# Syntax:
public virtual void GetObjectData(
SerializationInfoinfo,
StreamingContextcontext
);
Parameters:

info

The SerializationInfo that holds the serialized object data about the exception being thrown.

context

The StreamingContext that contains contextual information about the source or destination.
Exceptions
Exception TypeCondition
ArgumentNullException The info parameter is a null reference (not set or empty).
Implements:
ISerializable.GetObjectData
Remarks
GetObjectData sets a SerializationInfo with all the exception object data targeted for serialization. During deserialization, the exception is reconstituted from the SerializationInfo transmitted over the stream.
See also:
SerializationInfo | StreamingContext

Return to top

Method: ()
Inherited
See base class member description: System.Object.GetType
C# Syntax:
public Type GetType();

For more information on members inherited from System.Object click on the link above.

Return to top

Method: ()
Inherited
See base class member description: System.Object.MemberwiseClone
C# Syntax:
protected object MemberwiseClone();

For more information on members inherited from System.Object click on the link above.

Return to top

Method: ()
Inherited
See base class member description: System.Exception.ToString

Summary
Creates and returns a string representation of the current exception.
C# Syntax:
public override string ToString();
Return Value:
A string representation of the current exception.
RemarksToString returns a representation of the current exception that is intended to be understood by humans. Where the exception contains culture-sensitive data, the string representation returned by ToString is required to take into account the current system culture. Although there are no exact requirements for the format of the returned string, it should attempt to reflect the value of the object as perceived by the user.

The default implementation of Exception.ToString obtains the name of the class that threw the current exception, the message, the result of calling ToString on the inner exception, and the result of calling Environment.StackTrace. If any of these members is a null reference (not set or empty), its value is not included in the returned string.

If there is no error message or if it is an empty string ('), then no error message is returned. The name of the inner exception and the stack trace are returned only if they are not a null reference.

This method overrides Object.ToString.


Notes to inheritors: It is recommended, but not required, that ToString be overridden to return information about members declared in the derived class. For example, the ArgumentException class implements ToString so that it returns the value of the ParamName property, if that value is not a null reference.Example

Vb.net Ex.innerexception

The following example causes an exception and displays the result of calling ToString on that exception.

This code has the following output:

Error: System.ArgumentException: Object must be of type String. at System.String.CompareTo(Object value) at ArgExceptionExample.Main()

Return to top

Vb.net Ex.innerexception.message

Top of page

Vb.net Inner Exception Example

Copyright (c) 2002 Microsoft Corporation. All rights reserved.