Sunday, September 27, 2009

How handle exception in VB.NET ?

VB.NET has an inbuilt class that deals with errors. The Class is called Exception. When an exception error is found, an Exception object is created. The coding structure VB.NET uses to deal with such Exceptions is called the Try … Catch structure.

Syntax:TryCatch ex As ExceptionEnd TryThe Try word means "Try to execute this code". The Catch word means "Catch any errors here". The ex is a variable, and the type of variable it is an Exception object.

Example:Tryrt1.LoadFile ("C:\test10.txt", RichTextBoxStreamType.PlainText)Catch ex As Exception MsgBox (ex. Message) End Try

When you run your program, VB.net will try to execute any code in the Try part. If everything goes well, then it skips the Catch part. However, if an error occurs, VB.NET jumps straight to Catch and exception message in message box is executed.

No comments:

Computers Add to Technorati Favorites Programming Blogs - BlogCatalog Blog Directory