Exforsys

VB.NET 2005

  1. VB.NET 2005 Free Training
  2. The .NET Framework Architecture Part 1
  3. The .NET Framework Architecture Part 2
  4. Application Class and Message Class
  5. Implementing Class Library Object
  6. Visual Studio.NET Namespaces
  7. .NET Assemblies
  8. Differences between VB.NET 1.0 and VB.NET 2.0
  9. Introducing VB.NET Windows Forms
  10. Visual Studio Windows Forms Designer
  11. Exploring the Forms Designer generated code
  12. Setting and Adding Properties to Windows Form
  13. Implementing Inheritance
  14. Event Handling In Visual Basic .NET
  15. Building Graphical Interface elements
  16. .NET Common Windows Forms Controls Part 1
  17. .NET Common Windows Forms Controls Part 2
  18. Common Controls and Handling Control Events
  19. DomainUpDown and NumericUpDown Controls
  20. Dialog Boxes in Visual Basic .NET
  21. Visual Studio Adding Controls to Windows Form
  22. VB.NET Validation Controls
  23. Working with Menu Controls
  24. VB.NET MDI Applications
  25. .NET Exceptions
  26. VB.NET Creating and Managing Components Part 1
  27. VB.NET Creating and Managing Components Part 2
  28. Simple Data Binding
  29. .NET Complex Data Binding
  30. .NET Data Form Wizard
  31. Data Manipulation with ADO.NET
  32. SQL Server Stored Procedures
  33. SQL Server Ad Hoc Queries
  34. Finding and Sorting Data in DataSets
  35. ADO.NET Object Model
  36. Working with DataSets
  37. Using XML Data
  38. Working with File System in .NET
  39. Creating Web Service
  40. Instantiating - Invoking Web Services, Creating Proxy Classes with WSDL
  41. Web Reference and Web Services
  42. Web Services - SOAP, WSDL, Disco and UDDI
  43. Web Application Testing in VB.NET 2005
  44. Web Application Tracing and Debugging
  45. Working with Legacy Code and COM Components
  46. ActiveX Controls and Legacy Code
  47. Windows Application Testing
  48. VB.NET Windows Application Testing
  49. Tracing VB.NET Windows Application
  50. Debugging Windows Applications In Visual Studio.NET 2005
  51. Deploying Windows Applications In Visual Studio.NET 2005
  52. Customizing Setup Project in Visual Studio.NET 2005
  53. Shared Assembly
  54. Microsoft .NET Creating Installation Components
  55. The Registry Editor in Visual Studio.NET 2005
  56. The File Types Editor

Ads


Home arrow Technical Training arrow VB.NET 2005

.NET Exceptions

Page 1 of 2
Author : Exforsys Inc.     Published on: 5th Jul 2005    |   Last Updated on: 24th Dec 2007

.NET Exceptions

In this tutorial you will learn about Exceptions, Common Exceptions, Handling Exceptions - Try Block, Catch Block, Throw Statement, Finally Block, Salient points about error handling Custom Exceptions - Managing Unhandled Exception

Ads

Exceptions

Abnormal conditions can become obstacles in the execution of very good programs. These conditions may force the program to breakdown, or halt or just go into a limbo. The Network connection may snap or a printer may run out of paper. No programmer can foresee these problems, yet he must give the user an option to gracefully save the work done up to the point and exit the program. The Exception Management Application Block provides a simple yet extensible framework for handling exceptions in VB.NET.

What is an Exception?

Programs are designed to execute normally in predefined conditions. However, the program may occasionally be confronted with an abnormal situation which it is not built to handle. Such a situation will result in an “exception” to the norm. Programs will have to be told what it has to do when it encounters such abnormal conditions. The process of anticipating and providing for handling such extraordinary situations is known as exception handling. In .Net Framework 2005 we have a facility to break, correct the code and continue in case of exception. This facility was withdrawn in the earlier releases of .Net Framework.

An exception may occur due to a logical or a syntax error. Where syntax errors may be caught by the complier, logical errors are likely to be missed Therefore, you may have a program that may be trying to delete a file that does not exist or it may be trying to divide by zero or it may just be a null pointer error or a System IO exception. The program must be told what to do and how to do it. Some of these code level bugs come to light only at the time of execution or under extreme conditions. Most programmers as a matter of caution create exceptions and raise them on some predefined conditions to monitor the working of the program. They ensure that they provide for exception handling for the most commonly encountered problems and situations. Microsoft .NET framework 2005 has made this task of the programmer easier by building into the application the Exception Assistant.

An example of an exception handler code is illustrated below for your understanding:

Click here for Sample Code

In the above example a class has been instantiated and the SecondMethod is called. In the above code two variables a and b are passed on to the SecondMethod and a process is initiated in the SUB. Once this process is complete the SUB FirstMethod is called. The expected condition here is that the value of ‘n’ should not be zero. If at this point the value of “n” is set to zero the program will breakdown or terminate.

The Exception Assistant helps the programmer troubleshoot exceptions and also helps him understand more about the exceptions.

Some of the most common exceptions are listed below:

  • Code Access Security Exceptions
  • MicroSoft.Tools.CannotRemoveControlException
  • System.AccessViolationException
  • System.ArrayTypeMismatchExceptionSystem.Data.OleDb.OleDbException
  • System.Data.ConstraintException
  • System.Data.InvalidExpressionExcepton
  • System.Data.NoNull.AllowedException
  • System.Data.OdbcExcepton
  • System.Data.OracleClient.OracleException
  • System.DivideByZeroException
  • System.DuplicateWaitObjectException
  • System.IO.DirectroyNotFoundException
  • System.IO.EndOfStreamException
  • System.IO.FileNotFoundException
  • System.IO.IOException
  • System.IO.PathTooLongException
  • System.IO.InternalBufferOverflowException
  • System.NotSupportedException
  • NullReferenceException
  • System.ObjectDisposedException
  • System.OperationCalnceledException
  • System.OutOfMemoryException
  • System.OverflowException
  • System.Security.SecurityException
  • System.StackOverFlowException
  • System.UnauthorizedException

Ads

Handling Exceptions

Visual Basic supports both Structured and unstructured exception handling. If an exception occurs in a method that is not equipped to handle it, the exception is propagated back to the calling method, or the previous method. If the previous method also has no exception handler, the exception is propagated back to that method's caller, and so on. The search for a handler continues up the call stack, which is the series of procedures called within the application. If it fails to find a handler for the exception, an error message is displayed and the application is terminated.

The On Error statement is used specifically for unstructured exception handling. In unstructured exception handling, On Error is placed at the beginning of a block of code. It then has "scope" over that block; it handles any errors occurring within the block. If the program encounters another On Error statement, that statement becomes valid and the first statement becomes invalid.

In the Structured Exception Handling, the blocks of code are encapsulated with each block having one or more associated handlers. Each handler specifies some form of filter condition on the type of exception it handles. When an exception is raised by code in a protected block, the set of corresponding handlers is searched in order, and the first one with a matching filter condition is executed. A single method can have multiple structured exception handling blocks, and the blocks can also be nested within each other.

Using the Try...Catch...Finally statement, you can protect blocks of code that have the potential to raise errors. You can nest exception handlers, and the variables declared in each block will have local scope.



 
This tutorial is part of a VB.NET 2005 tutorial series. Read it from the beginning and learn yourself.

VB.NET 2005

  1. VB.NET 2005 Free Training
  2. The .NET Framework Architecture Part 1
  3. The .NET Framework Architecture Part 2
  4. Application Class and Message Class
  5. Implementing Class Library Object
  6. Visual Studio.NET Namespaces
  7. .NET Assemblies
  8. Differences between VB.NET 1.0 and VB.NET 2.0
  9. Introducing VB.NET Windows Forms
  10. Visual Studio Windows Forms Designer
  11. Exploring the Forms Designer generated code
  12. Setting and Adding Properties to Windows Form
  13. Implementing Inheritance
  14. Event Handling In Visual Basic .NET
  15. Building Graphical Interface elements
  16. .NET Common Windows Forms Controls Part 1
  17. .NET Common Windows Forms Controls Part 2
  18. Common Controls and Handling Control Events
  19. DomainUpDown and NumericUpDown Controls
  20. Dialog Boxes in Visual Basic .NET
  21. Visual Studio Adding Controls to Windows Form
  22. VB.NET Validation Controls
  23. Working with Menu Controls
  24. VB.NET MDI Applications
  25. .NET Exceptions
  26. VB.NET Creating and Managing Components Part 1
  27. VB.NET Creating and Managing Components Part 2
  28. Simple Data Binding
  29. .NET Complex Data Binding
  30. .NET Data Form Wizard
  31. Data Manipulation with ADO.NET
  32. SQL Server Stored Procedures
  33. SQL Server Ad Hoc Queries
  34. Finding and Sorting Data in DataSets
  35. ADO.NET Object Model
  36. Working with DataSets
  37. Using XML Data
  38. Working with File System in .NET
  39. Creating Web Service
  40. Instantiating - Invoking Web Services, Creating Proxy Classes with WSDL
  41. Web Reference and Web Services
  42. Web Services - SOAP, WSDL, Disco and UDDI
  43. Web Application Testing in VB.NET 2005
  44. Web Application Tracing and Debugging
  45. Working with Legacy Code and COM Components
  46. ActiveX Controls and Legacy Code
  47. Windows Application Testing
  48. VB.NET Windows Application Testing
  49. Tracing VB.NET Windows Application
  50. Debugging Windows Applications In Visual Studio.NET 2005
  51. Deploying Windows Applications In Visual Studio.NET 2005
  52. Customizing Setup Project in Visual Studio.NET 2005
  53. Shared Assembly
  54. Microsoft .NET Creating Installation Components
  55. The Registry Editor in Visual Studio.NET 2005
  56. The File Types Editor
 

Comments