熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

ASP.NET入門教程 14.1.1 對象處理[2]

2022-06-13   來源: .NET編程 

    使用Try/Catch進行處理

    為了使用Try/Catch方法進行處理需要遵循下面這個簡單模式

 

Try

           creat resource

         Catch

            handle exception

         Finally

           dispose of resource

         End Try

    使用這種模式的圖像代碼如下

 

 Public Shared Sub GenerateThumbnail(ByVal SourceImagePath As String ByVal TargetImagePath As String)

 

        Dim newHeight As Short

        Dim newWidth As Short

                   Dim sourceImage As Image=Nothing

                   Dim targetImage As Image=Nothing

                  

                   Try

 

        sourceImage = ImageFromFile(SourceImagePath)

 

            newHeight = CShort(sourceImageHeight * )

            newWidth = CShort(sourceImageWidth * )

 

            Dim cb As New ImageGetThumbnailImageAbort(AddressOf ThumbnailCallback)

            Try

                                targetImage= sourceImageGetThumbnailImage(newWidth newHeight cb IntPtrZero)

                targetImageSave(TargetImagePath ImagingImageFormatGif)

                              Catch ex As Exception

                              Finally

                                If targetImage IsNot Nothing Then

                                       targetImagedispose()

                                      End If

                              End Try

                    Catch ex As Exception

                    Finally

                           If sourceImage IsNot Nothing Then

                              sourceImageDispose()

                            End If

                     End Try

 

    End Sub

    可以立刻看到這段代碼非常難以讀懂有兩個Try/Catch塊具有嵌套結構外部的Try/Catch塊用於SourceImage:原始圖像使用ImageFromFile從文件中加載該圖像然後使用源圖像的Height和Width屬性計算新的高度和寬度新的高度和寬度是原始圖像的%在定義新的尺寸後如果在創建縮略圖期間產生錯誤則創建回調變量(cb)如果發生錯誤GetThumbnaillmage方法將調用該回調實際上沒有處理任何錯誤因為決定是否生成縮略圖並不是至關重要的部分如果具有一個應用程序其中至關重要的部分是了解這些錯誤則可以在回調過程中記錄這些錯誤

[]  []  []  []  


From:http://tw.wingwit.com/Article/program/net/201311/15273.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.