Working with Exceptions in Visual Studio .NET

Encoding QR-Code in Visual Studio .NET Working with Exceptions

Working with Exceptions
Making QR Code ISO/IEC18004 In Visual Studio .NET
Using Barcode creator for Visual Studio .NET Control to generate, create QR image in Visual Studio .NET applications.
www.OnBarcode.com
Recognize QR Code 2d Barcode In Visual Studio .NET
Using Barcode scanner for Visual Studio .NET Control to read, scan read, scan image in .NET framework applications.
www.OnBarcode.com
Most .NET exceptions are automatically translated to the corresponding HRESULT codes when they are marshaled back to COM clients. For example, a DivideByZeroEx ception object is translated to an HRESULT code equal to COR_E_DIVIDEBYZERO, which a Visual Basic 6 client can trap with an On Error statement and interpret as an error whose Err.Number code is 11. In those rare cases when you need to return a more specific HRESULT code, you can use a couple of techniques. The first and simplest one relies on the ThrowExceptionForHR method of the Marshal class:
Barcode Maker In VS .NET
Using Barcode creator for VS .NET Control to generate, create barcode image in Visual Studio .NET applications.
www.OnBarcode.com
Read Barcode In .NET
Using Barcode recognizer for VS .NET Control to read, scan read, scan image in Visual Studio .NET applications.
www.OnBarcode.com
Throw an exception whose HRESULT is hex 80001234. Marshal.ThrowExceptionForHR(&H80001234)
Quick Response Code Printer In Visual C#.NET
Using Barcode creation for VS .NET Control to generate, create Denso QR Bar Code image in .NET framework applications.
www.OnBarcode.com
Make QR Code ISO/IEC18004 In Visual Studio .NET
Using Barcode generator for ASP.NET Control to generate, create QR Code 2d barcode image in ASP.NET applications.
www.OnBarcode.com
The second, more elegant technique consists of the definition of a custom exception class whose constructor assigns the desired error code to its HResult protected property:
Denso QR Bar Code Encoder In Visual Basic .NET
Using Barcode creation for .NET framework Control to generate, create QR image in Visual Studio .NET applications.
www.OnBarcode.com
Drawing ANSI/AIM Code 39 In VS .NET
Using Barcode maker for VS .NET Control to generate, create Code 39 Full ASCII image in .NET applications.
www.OnBarcode.com
Public Class CustomException Inherits Exception Sub New(ByVal message As String) MyBase.New(message) Me.HResult = &H80001234 End Sub End Class
UPC-A Creation In .NET
Using Barcode generation for .NET framework Control to generate, create UPC A image in Visual Studio .NET applications.
www.OnBarcode.com
Barcode Printer In .NET
Using Barcode generation for VS .NET Control to generate, create bar code image in VS .NET applications.
www.OnBarcode.com
Your .NET class can then throw an exception with a given HRESULT by throwing this custom exception as it would any standard exception:
PDF-417 2d Barcode Creation In .NET Framework
Using Barcode drawer for Visual Studio .NET Control to generate, create PDF 417 image in VS .NET applications.
www.OnBarcode.com
Make Postnet In .NET
Using Barcode creator for .NET Control to generate, create Delivery Point Barcode (DPBC) image in .NET framework applications.
www.OnBarcode.com
Throw New CustomException( File not found )
Make Code 3 Of 9 In Visual Basic .NET
Using Barcode generation for VS .NET Control to generate, create ANSI/AIM Code 39 image in .NET framework applications.
www.OnBarcode.com
QR Code JIS X 0510 Creation In Java
Using Barcode creation for Android Control to generate, create QR Code ISO/IEC18004 image in Android applications.
www.OnBarcode.com
Writing COM-Friendly .NET Components
Read Barcode In Visual Basic .NET
Using Barcode recognizer for .NET framework Control to read, scan read, scan image in VS .NET applications.
www.OnBarcode.com
Painting Code-128 In Java
Using Barcode maker for BIRT Control to generate, create Code 128 image in Eclipse BIRT applications.
www.OnBarcode.com
.NET components that are meant to be exposed to COM clients shouldn t use features that COM-based clients can t see. Or they should provide alternative ways for COM
Code 128A Reader In None
Using Barcode decoder for Software Control to read, scan read, scan image in Software applications.
www.OnBarcode.com
Scan PDF 417 In C#.NET
Using Barcode decoder for Visual Studio .NET Control to read, scan read, scan image in .NET framework applications.
www.OnBarcode.com
Part VII:
Generate Barcode In Java
Using Barcode maker for Java Control to generate, create barcode image in Java applications.
www.OnBarcode.com
UPC-A Supplement 2 Creator In VB.NET
Using Barcode creator for VS .NET Control to generate, create UPC-A image in Visual Studio .NET applications.
www.OnBarcode.com
Advanced Topics
clients to access those features. Here s a brief summary of the dos and don ts of COMfriendly .NET components:
Only public and nonabstract classes can be exposed to COM; use ComVisi ble(False) for public MustInherit classes. Avoid deep hierarchies in .NET classes, such as nested classes or namespaces with more than two levels. The class must expose an implicit or explicit parameterless constructor; construc tors with parameters can t be accessed via COM Interop. The class shouldn t expose shared members, as they aren t visible to COM clients. The class shouldn t expose overloaded members because they can create confu sion when used by COM clients. If the class exposes events, define them in a separate interface, and use the ComSourceInterfaces attribute to let COM Interop export them correctly. For simplicity s sake, use the ComClass attribute if COM clients don t need to access fields, trap events, or invoke methods inherited from System.Object. Use custom exception classes that set the HResult property for returning nonstandard error codes to COM clients.
The .NET Framework offers superb support for the transition from the unmanaged world to the .NET world, thanks to PInvoke and COM Interop. In most cases, these two worlds can communicate quite easily, but you must be familiar with the techniques I have covered in this chapter to solve some of the problems you might bump into when writing real-world applications. However, never forget that a .NET application that relies on COM components inherits many of the issues that have plagued COM pro gramming for example, the tendency to leak memory. There are other ways to leverage pre-.NET technologies. One of them is using serviced components that build on COM+ services, as I explain in the next chapter.
31 Serviced Components
As you might remember from 1, the Microsoft .NET Framework doesn t replace COM+ and the built-in features portion of the Microsoft Windows operating system known as Component Services. As a matter of fact, COM+ has been greatly improved even after the inception of .NET and has evolved from version 1.0 (the ver sion provided with Windows 2000) to version 1.5 (the version introduced with Win dows XP and enhanced with Windows Server 2003). You can leverage COM+ features from a .NET application by writing one or more serviced components.
A Quick Introduction to COM+
Many books have been written about COM+ and I can t even begin to do this technology justice in just one chapter. Nevertheless, an introduction is in order for those program mers who have never worked with COM+ in previous versions of Microsoft Visual Basic. The COM+ story began when Microsoft released the Option Pack for Windows NT; among its other products, that Option Pack included Microsoft Transaction Server (MTS), which enabled you to instantiate and invoke a component hosted in a DLL, on either the same or another computer. The most intriguing new feature was the ability to create transactional components, which can perform transactions that spawn different databases. A few years later, Microsoft released Windows 2000, which included a new module offi cially named Component Services but that is known among developers as COM+. This operating system module offered everything MTS did and a lot more, including object pooling, queued components, and loosely coupled events. At last developers could cre ate enterprise-level, distributed applications with relatively little effort, as they could rely on the COM+ infrastructure for services such as distributed transactions, security, syn chronization, resource management, and deployment. Programmers could focus on building the application s logic, thus saving a lot of time in development and debugging. Windows XP and Windows Server 2003 come with version 1.5 of COM+, which adds new features, such as the ability to access a COM+ component via SOAP (as if it were a Web service) and the ability to run a component as a Windows NT service. You can adjust the isolation level used by transactional components, register a given compo nent under multiple COM+ applications, and create application partitions. COM+ appli cations can be configured to automatically restart after a given timeout or after a given number of objects are created. Regardless of the Windows version you re using, you can administer COM+ by means of the Component Services snap-in, an MMC applet that you can reach from the Administrative Tools menu. (See Figure 31-1.) You can create a custom MMC
Part VII:
Copyright © OnBarcode.com . All rights reserved.