Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

FREE 12 month online training for ASP.NET & MS Expression Studio and a Free copy of MS Expression Web with Windows Server Purchase
Error Handling in VB.Net as(Try,Catch,Finally).

Author: DevASP
Download Source Code : 567_TRY CATCH FiNAlly.zip

In this article I will show you the comparison between different error handling Schemes and the advantage of try catch Error Handling.

This solution demonstrates the new Try, Catch, Finally error handling in Visual Basic .NET. Press F5 to start the application in Debug mode using the breakpoints. Press Ctrl+F5 to run the program and ignore the breakpoints.

There are 5 command buttons to test. Each one tries to open the file specified in the text box labeled 'Text File To Open.' Each button (except the one labeled 'No Error Handling') uses various degrees of error handling using Try, Catch, and Finally blocks.

First Button is No Try Catch .By Double clicking u will find the routine like below.so write the as mention below.

It infact Ask to make sure the user is willing to possibly blow up the program. Use the FileStream class from the System.IO Namespace (see Imports at top of file). This command will fail if the file does not exist.

 

Private Sub cmdNoTryCatch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdNoTryCatch.Click

                        Dim strMsg As String = "The following code has no error handling and will cause an unhandled exception if a file is not found. Do you want to continue?"

                        If MessageBox.Show(strMsg, Me.Text, MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) = DialogResult.Yes Then

                                    Dim fs As FileStream

 

                                    fs = File.Open(Me.txtFileName.Text, FileMode.Open)

                                    MessageBox.Show("The size of the file is: " & fs.Length, Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Information)

                                    fs.Close()

End If

 

This procedure will perform a basic Try, Catch. Use the FileStream class from the System.IO Namespace (see Imports at top of file).This command will fail if the file does not exist.it Will catch any error that we're not explicitly trapping.

 

Private Sub cmdBasicTryCatch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdBasicTryCatch.Click

                        Dim fs As FileStream

 

                        Try

                                    fs = File.Open(Me.txtFileName.Text, FileMode.Open)

                                    MessageBox.Show("The size of the file is: " & fs.Length, Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Information)

                                    fs.Close()

 

                        Catch exp As Exception

                                    MessageBox.Show(exp.Message, Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Stop)

                        End Try

            End Sub

 

This procedure will perform a specific Try, Catch looking for any IO related errors. Use the FileStream class from the System.IO Namespace (see Imports at top of file). This command will fail if the file does not exist. It Will catch an error when the file requested does not exist. it Will catch an error when the directory requested does not exist. it Will catch any generic IO exception and Also Will catch any error that we're not explicitly trapping.

 

 

Private Sub cmdDetailedTryCatch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdDetailedTryCatch.Click

                        Dim fs As FileStream

 

                        Try

                                                                        fs = File.Open(Me.txtFileName.Text, FileMode.Open)

                                    MessageBox.Show("The size of the file is: " & fs.Length, Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Information)

                                    fs.Close()

                        Catch exp As FileNotFoundException

                                    MessageBox.Show("The file you requested does not exist.", Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Stop)

 

                        Catch exp As DirectoryNotFoundException

                                    MessageBox.Show("The directory you requested does not exist.", Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Stop)

 

                        Catch exp As IOException

                                    MessageBox.Show(exp.Message, Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Stop)

 

                        Catch exp As Exception

           

                                    MessageBox.Show(exp.Message, Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Stop)

 

                        End Try

            End Sub

 

 

 

This procedure will perform a specific Try, Catch looking for any IO related errors Use the FileStream class from the System.IO Namespace (see Imports at top of file) This command will fail if the file does not exist.Will catch any generic IO exception You could use the StringWriter to build a multi-line string in memory. However, it's overkill for this simple message. FYI StringWriter comes from the System.IO Namespace. Accessing an exception objects StackTrace could cause an exception thus we need to wrap the call in its own Try, Catch block. This catch will trap any error unexpected error.

 

 

Private Sub cmdCustomMessage_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdCustomMessage.Click

                        Dim fs As FileStream

 

                        Try

                                                            fs = File.Open(Me.txtFileName.Text, FileMode.Open)

                                    MessageBox.Show("The size of the file is: " & fs.Length, Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Information)

                                    fs.Close()

 

                        Catch exp As IOException

                                    Dim strMsg As String

                                    strMsg = "I was unable to open the file you requested, " & Me.txtFileName.Text & vbCrLf & vbCrLf & _

                                      "Detailed Error Information below:" & vbCrLf & vbCrLf & _

                                      "  Message: " & exp.Message & vbCrLf & _

                                      "  Source: " & exp.Source & vbCrLf & vbCrLf & _

                                      "  Stack Trace:" & vbCrLf

 

                                    Dim strStackTrace As String

 

                                    Try

                                                strStackTrace = exp.StackTrace()

                                    Catch stExp As Security.SecurityException

                                                strStackTrace = "Unable to access stack trace due to security restrictions."

 

                                    Catch stExp As Exception

                                                strStackTrace = "Unable to access stack trace."

                                    End Try

 

                                    strMsg = strMsg & strStackTrace

 

                                    MessageBox.Show(strMsg, Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Stop)

 

                        Catch exp As System.Exception

                                    MessageBox.Show(exp.Message, Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Stop)

 

                        End Try

            End Sub

When you run the example from within Visual Studio .NET, the debugger will be invoked via breakpoints for each button. You can selectively turn the breakpoints on and off by using the Breakpoints Window, accessible via the Debug | Windows | Breakpoints menu command or the CTRL+ALT+B keystroke.

In the code samples, the new .NET Framework version of MsgBox (MessageBox from the System.Windows.Forms namespace) is used. In Visual Basic .NET applications, MsgBox will continue to work fine. However, C# and other languages do not expose MsgBox. Use MessageBox instead in those cases.

Article Comments
how i can get the error line number in vb.net

plz help asap

thanx in advance
vishnu

Posted on 3/5/2007 12:48:57 PM by vishnu

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< Creating Screen Saver Using Vb.Net.(Part 2)

Disclaimer - Privacy
© 2002-2014 DevASP.net