Exceptions in .Net

I found this snippet on another site, and it looks like it comes from the book I just bought about building frameworks from the guys that worked on the .net framework.

  • DO report execution failures by throwing exceptions.
  • CONSIDER terminating the process by calling System.Environment.FailFast (.NET 2.0) instead of throwing an exception, if your code reaches a situation where you consider it unsafe for further execution.
  • DO NOT use exceptions for the normal flow of control, if possible.
  • CONSIDER the performance implications of throwing exceptions.
  • DO document all exceptions thrown by publicly callable members because of a violation of the member contract and treat them as part of your contract.
  • DO NOT have public members that can either throw or not based on some option such as a bool parameter (.e.g., bool throwOnError)
  • DO NOT have public members that return exceptions as the return value or as an out parameter.
  • CONSIDER using exception builder methods.
  • DO NOT throw exceptions from exception filter blocks
  • AVOID explicitly throwing exceptions from finally blocks.
  • DO throw the most specific (the most derived) exception that makes sense.
  • DO NOT swallow errors by catching nonspecifc exceptions ( e.g., catch (Exception e) { } // swallowed whole!)
  • CONSIDER catching a specific exception when you understand why it was thrown in a given context and you can respond to the failure programmatically.
  • DO Clean up any side effects when throwing an exception. For example, if a Hashtable.Insert method throws an exception, the caller can assume that the specified item was not added to the Hashtable.
  • DO NOT derive all new exceptions directly from the base class SystemException. Inherit from SystemException only when creating new exceptions in System namespaces. Inherit from ApplicationException when creating new exceptions in other namespaces.
  • DO use the predefined exceptions types. Define new exception types only for programmatic scenarios.
  • DO NOT overcatch. Exceptions should often simply be allowed to propagate up the call stack.
  • DO prefer using an empty throw when catching and rethrowing an exception. This is the best way to preserve the exception call stack.
  • AVOID creating custom exception classes when there is already an exception type that’s “good enough”.
  • DO Design classes so that in the normal course of use an exception will never be thrown.
  • DO NOT return Error Codes!
  • Increase Concurrent Downloads in IE

    The HTTP spec specifies that you can not have more than 2 transfers when using HTTP 1.1, or more than 4 transfers in HTTP 1.0.

    So what this means is that you can’t download a bunch of files from the same server at the same time.

    More info on this problem can be found at http://support.microsoft.com/kb/183110.

    You can configure WinInet to exceed this limit by creating and setting the following registry entries:

    Note By changing these settings, you cause WinInet to go against the HTTP protocol specification recommendation. You should only do this if absolutely necessary and then you should avoid doing standard Web browsing while these settings are in effect:

    HKEY_CURRENT_USERSoftwareMicrosoftWindowsCurrentVersionInternet Settings

    MaxConnectionsPerServer REG_DWORD (Default 2)
    Sets the number of simultaneous requests to a single HTTP 1.1 Server

    MaxConnectionsPer1_0Server REG_DWORD (Default 4)
    Sets the number of simultaneous requests to a single HTTP 1.0 Server

    These settings are made for a particular user and will have no affect on other users who log on to the computer.

    In Internet Explorer 5, it is possible to change the connection limit programmatically by calling the InternetSetOption function on NULL handle with the following flags (note that it will change connection limit for the whole process):

    INTERNET_OPTION_MAX_CONNS_PER_SERVER INTERNET_OPTION_MAX_CONNS_PER_1_0_SERVER

    Note If the process has established a connection to a server, if you change the connection limit by calling InternetSetOption, the function does not have any effect to subsequent connections on the same server. This occurs even if a previous connection is disconnected prior to the call to InternetSetOption. Connection limit does affect all other servers.

    Keywords: IE Downloads, Concurrent Downloads, 2 downloads

    Printing a VB.Net Form

    I found this useful code at VB-Helper.com

     

    .csharpcode
    {
    font-size: small;
    color: black;
    font-family: Courier New , Courier, Monospace;
    background-color: #ffffff;
    /*white-space: pre;*/
    }

    .csharpcode pre { margin: 0em; }

    .csharpcode .rem { color: #008000; }

    .csharpcode .kwrd { color: #0000ff; }

    .csharpcode .str { color: #006080; }

    .csharpcode .op { color: #0000c0; }

    .csharpcode .preproc { color: #cc6633; }

    .csharpcode .asp { background-color: #ffff00; }

    .csharpcode .html { color: #800000; }

    .csharpcode .attr { color: #ff0000; }

    .csharpcode .alt
    {
    background-color: #f4f4f4;
    width: 100%;
    margin: 0em;
    }

    .csharpcode .lnum { color: #606060; }

    #Region "Print"
        Private Declare Auto Function BitBlt Lib "gdi32.dll" (ByVal _
        hdcDest As IntPtr, ByVal nXDest As Integer, ByVal _
        nYDest As Integer, ByVal nWidth As Integer, ByVal _
        nHeight As Integer, ByVal hdcSrc As IntPtr, ByVal nXSrc _
        As Integer, ByVal nYSrc As Integer, ByVal dwRop As _
        System.Int32) As Boolean
        Private Const SRCCOPY As Integer = &HCC0020
    
        ' Variables used to print.
        Private m_PrintBitmap As Bitmap
        Private WithEvents m_PrintDocument As Printing.PrintDocument
    
        Private Sub PrintForm()
            ' Copy the form's image into a bitmap.
            m_PrintBitmap = GetFormImage()
    
            ' Make a PrintDocument and print.
            MsgBox(m_PrintDocument.PrinterSettings.PrinterName)
            m_PrintDocument = New PrintDocument
            m_PrintDocument.Print()
        End Sub
    
        Private Function GetFormImage() As Bitmap
            ' Get this form's Graphics object.
            Dim me_gr As Graphics = Me.CreateGraphics
    
            ' Make a Bitmap to hold the image.
            Dim bm As New Bitmap(Me.ClientSize.Width, _
                Me.ClientSize.Height, me_gr)
            Dim bm_gr As Graphics = Graphics.FromImage(bm)
            Dim bm_hdc As IntPtr = bm_gr.GetHdc
    
            ' Get the form's hDC. We must do this after 
            ' creating the new Bitmap, which uses me_gr.
            Dim me_hdc As IntPtr = me_gr.GetHdc
    
            ' BitBlt the form's image onto the Bitmap.
            BitBlt(bm_hdc, 0, 0, Me.ClientSize.Width, _
                Me.ClientSize.Height, _
                me_hdc, 0, 0, SRCCOPY)
            me_gr.ReleaseHdc(me_hdc)
            bm_gr.ReleaseHdc(bm_hdc)
    
            ' Return the result.
            Return bm
        End Function
    
        ' Print the form image.
        Private Sub m_PrintDocument_PrintPage(ByVal sender As _
            Object, ByVal e As _
            System.Drawing.Printing.PrintPageEventArgs) Handles _
            m_PrintDocument.PrintPage
            ' Draw the image centered.
            Dim x As Integer = e.MarginBounds.X + _
                (e.MarginBounds.Width - m_PrintBitmap.Width)  2
            Dim y As Integer = e.MarginBounds.Y + _
                (e.MarginBounds.Height - m_PrintBitmap.Height)  2
            e.Graphics.DrawImage(m_PrintBitmap, x, y)
    
            ' There's only one page.
            e.HasMorePages = False
        End Sub
    
    #End Region