[From msdn] how to map hresult and exception

Source: Internet
Author: User

The com method reports an error by returning hresult; the. NET method reports an error by raising an exception. The runtime will process the conversion between the two .. . NET Framework maps each exception class to an hresult.

User-defined exception classes can specify any appropriate hresult. WhenHresultWhen fields are used to generate exceptions, these exception classes can dynamically change the returned hresult. Other information about the exception is passedIerrorinfoThe interface is provided to the client, which is implemented on. NET objects in an unmanaged process.

If an extension is createdSystem. ExceptionYou must set the hresult field during the construction process. Otherwise, the base class will assign the hresult value. By providing the hresult value in the exception constructor, You can map the new exception class to the existing hresult.

Note that when a thread existsIerrorinfoThe Runtime Library sometimes ignoresHresult. This behavior may occur inHresultAndIerrorinfoIt does not indicate the same error. 

Create an exception class and map it to hresult
    • Use the followingCodeCreateNoaccessexceptionAnd map it to hresultE_accessdenied.

Class noaccessexception: Public Applicationexception
{
Noaccessexception (){
Hresult=E_accessdenied;
}
}
Cmyclass: methodthatthrows
{
Throw NewNoaccessexception ();
}

You may have encounteredProgram(Take anyProgramming LanguageWrite ). For example, in the following code example, use the custom SeparatorMarshal. throwexceptionforhr (INT hresult)Method to cause an exception with a specific hresult value. This method searches for this hresult and generates the corresponding exception type. For example, the hresult in the following code snippet will generateArgumentexception.

Cmyclass: methodthatthrows
{
Marshal. throwexceptionforhr (cor_e_argument );
}

The following table provides a complete link ing between each hresult and its comparable exception classes in. NET Framework.

Hresult . Net exception

Msee_e_appdomainunloaded

Appdomainunloadedexception

Cor_e_application

Applicationexception

Cor_e_argument or e_invalidarg

Argumentexception

Cor_e_argumentoutofrange

Argumentoutofrangeexception

Cor_e_arithmetic or error_arithmetic_overflow

Arithmeticexception

Cor_e_arraytypemismatch

Arraytypemismatchexception

Cor_e_badimageformat or error_bad_format

Badimageformatexception

Cor_e_comemulate_error

Comemulateexception

Cor_e_contextmarshal

Contextexternalexception

Cor_e_core

Coreexception

Nte_fail

Cryptographicexception

Cor_e_directorynotfound or error_path_not_found

Directorynotfoundexception

Cor_e_dividebyzero

Dividebyzeroexception

Cor_e_duplicatewaitobject

Duplicatewaitobjectexception

Cor_e_endofstream

Endofstreamexception

Cor_e_typeload

Entrypointnotfoundexception

Cor_e_exception

Exception

Cor_e_executionengine

Executionengineexception

Cor_e_fieldaccess

Fieldaccessexception

Cor_e_filenotfound or error_file_not_found

Filenotfoundexception

Cor_e_format

Formatexception

Cor_e_indexoutofrange

Indexoutofrangeexception

Cor_e_invalidcast or e_nointerface

Invalidcastexception

Cor_e_invalidcomobject

Invalidcomobjectexception

Cor_e_invalidfiltercriteria

Invalidfiltercriteriaexception

Cor_e_invalidolevarianttype

Invalidolevarianttypeexception

Cor_e_invalidoperation

Invalidoperationexception

Cor_e_io

Ioexception

Cor_e_memberaccess

Accessexception

Cor_e_methodaccess

Methodaccessexception

Cor_e_missingfield

Missingfieldexception

Cor_e_missingmanifestresource

Missingmanifestresourceexception

Cor_e_missingmember

Missingmemberexception

Cor_e_missingmethod

Missingmethodexception

Cor_e_multicastnotsupported

Multicastnotsupportedexception

Cor_e_notfinitenumber

Notfinitenumberexception

E_notimpl

Notimplementedexception

Cor_e_notsupported

Notsupportedexception

Cor_e_nullreference or e_pointer

Nullreferenceexception

Cor_e_outofmemory or

E_outofmemory

Outofmemoryexception

Cor_e_overflow

Overflowexception

Cor_e_pathtoolong or error_filename_exced_range

Pathtoolongexception

Cor_e_rank

Rankexception

Cor_e_reflectiontypeload

Reflectiontypeloadexception

Cor_e_remoting

Remotingexception

Cor_e_safearraytypemismatch

Safearraytypemismatchexception

Cor_e_security

Securityexception

Cor_e_serialization

Serializationexception

Cor_e_stackoverflow or error_stack_overflow

Stackoverflowexception

Cor_e_synchronizationlock

Synchronizationlockexception

Cor_e_system

Systemexception

Cor_e_target

Targetexception

Cor_e_targetinvocation

Targetinvocationexception

Cor_e_targetparamcount

Targetparametercountexception

Cor_e_threadaborted

Threadabortexception

Cor_e_threadinterrupted

Threadinterruptedexception

Cor_e_threadstate

Threadstateexception

Cor_e_threadstop

Threadstopexception

Cor_e_typeload

Typeloadexception

Cor_e_typeinitialization

Typeinitializationexception

Cor_e_verification

Verificationexception

Cor_e_weakreference

Weakreferenceexception

Cor_e_vtablecallsnotsupported

Vtablecallsnotsupportedexception

All other hresults

Comexception

To retrieve extended error information, the hosting client must check the fields of the generated exception object. To make the exception object provide useful information about the error, the COM object must be implementedIerrorinfoInterface. Running database usageIerrorinfoProvided information to initialize the exception object.

If the COM object is not supportedIerrorinfo, The Runtime Library will use the default value to initialize the exception object. The following table lists the fields associated with the exception object.IerrorinfoDefault information source.

Note: When ierrorinfo exists on the thread, the runtime database sometimes ignores hresult . This behavior may occur when hresult and ierrorinfo do not indicate the same error.

The string returned by The string returned by
exception field com Information Source

errorcode

call the returned hresult.

helplink

If ierrorinfo-> helpcontext is not zero, strings are formed by concatenating ierrorinfo-> gethelpfile , "#", and ierrorinfo-> gethelpcontext . Otherwise, the string is returned from ierrorinfo-> gethelpfile .

innerexception

always blank reference ( nothing in Visual Basic ).

message

ierrorinfo-> getdescription .

source

ierrorinfo-> getsource .

stacktrace

stack trace.

targetsite

name of the method that returns the failed hresult.

Message,SourceAndStacktraceAnd other exception fields cannot be usedStackoverflowexception.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.