Presentation is loading. Please wait.

Presentation is loading. Please wait.

By Sam Nasr, MCP May 22, 2007 www.ClevelandDotNet.info.

Similar presentations


Presentation on theme: "By Sam Nasr, MCP May 22, 2007 www.ClevelandDotNet.info."— Presentation transcript:

1 By Sam Nasr, MCP May 22, 2007

2  Introduction  Exception Handling  Break  Open Forum  Q&A  Survey

3  Used to handle errors  Uses Try/Catch/Finally blocks  Throw errors  Other classes derive from Exception{} base class

4  SQLException  SQLTypeException  DataException  SOAPException

5  Code: returns the SOAP fault code  Actor: identifies the code that causes the exception  Source: returns the name of the assembly where the application occurs  Detail: returns an XMLNode object that describes the exception.

6 Demo

7  Public Class EmployeeListNotFoundException  Inherits Exception  OR  public class MyFileNotFoundException :Exception  {  }

8  Programmatically check for a condition that is likely to occur without using exception handling. If conn.State <> ConnectionState.Closed Then conn.Close() End If As opposed to: Try conn.Close() Catch ex As InvalidOperationException 'Do something with the error or ignore it. End Try

9  Design classes so that an exception is never thrown in normal use.  Class FileRead  Sub Open()  Dim stream As FileStream = _ File.Open("myfile.txt", FileMode.Open)  Dim b As Byte  Dim result As Integer ' ReadByte returns -1 at EOF.  Do result = stream.ReadByte()  If result = -1 Then Exit Do  b = CByte(result)  ' Do something.  Loop  ' Call stream.Close() here or in another method.  End Sub 'Open  End Class 'FileRead

10  Throw exceptions instead of returning an error code or HRESULT.  Clean up intermediate results when throwing an exception.  Use grammatically correct error messages.  Provide Exception properties for programmatic access. Include extra information in an exception (in addition to the description string) only when there is a programmatic scenario where the additional information is useful.  Return null for extremely common error cases.

11  Use try/finally blocks around code that can potentially generate an exception and centralize your catch statements in one location.  Always order exceptions in catch blocks from the most specific to the least specific.  In most cases, use the predefined exceptions types.

12 MSDN Patterns & Practices MSDN Exception Handling Application Block Exception Handling Best Practices in.NET Try...Catch...Finally Statements

13


Download ppt "By Sam Nasr, MCP May 22, 2007 www.ClevelandDotNet.info."

Similar presentations


Ads by Google