An exception is an error condition or any abnormal condition in
the program and disrupts the normal flow of the program.
When we try to open a file ,if it does not exist, the exception will be raised and handled by the system itself. There are three types of errors:-
Syntax errors
Runtime errors
Logical errors Syntax Errors :-
Those errors which appear when we write the wrong syntax.
Runtime Errors:-
Those errors which appear only when we compile and run our code.
Logical Errors:-
Those errors which appear when we use the application, but
producing wrong results. Some of the system defined Exceptions are:- Exception- This is the base class for all exceptions. IndexoutofRange- This is raising when we try to access the element of an array, which is out of range. NullReferenceException- It raises when null object is referenced. ArgumentException- It raises when the arguments to the method are not valid. ArithmeticException- It raises when the arithmetic operation results an infinite output. SYNTAX:- Try { <statements> [catch[exception as type]] [catch[exception as type]] [catch[exception as type]] [finally [finally statements]] End Try ( Try block has to be followed by either catch or finally block. Even both can be allowed in a single block. The exceptions need to be caught by using try-catch , try – finally , and try-catch-finally block.)
Throw:- Using this, we can explicitly throw the exception
. It is helpful for throwing user defined exception also. Public Class Form3 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Try Dim a = 0, b = 10, c As Integer c=b\a Catch ex1 As ArithmeticException MsgBox("division by zero exception") Catch ex1 As Exception MsgBox(ex1.Message) End Try End Sub End Class Public Class Form4 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Dim a = 10, b = 10, c As Integer Dim name(3) As String Try c=b/a name(4) = "jack“ Catch ex As ArithmeticException MsgBox("Division by zero exception") Catch ex2 As IndexOutOfRangeException MsgBox("accessing array out of range") Catch ex1 As Exception MsgBox(ex1.Message) Finally MsgBox("from finally blocked") End Try End Sub End Class Public Class Form5 Private Sub Button1_Click(sender As Object, e As EventArgs) HandlesButton1.Click Dim a = 0, c As Integer Dim b As String = "kk“ Dim name(3) As String Try c=b/a name(4) = "jack" Catch ex As ArithmeticException MsgBox("Division by zero exception") Catch ex2 As IndexOutOfRangeException MsgBox("accessing array out of range") Catch ex1 As Exception MessageBox.Show(ex1.Message) Finally MsgBox("from finally blocked") End Try End Sub End Class Public Class Form6 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Dim b, c As String b = TextBox1.Text c = b.Chars(0) Try If Asc(c) < 65 Or Asc(c) > 90 Then Throw New ArithmeticException End If Catch ex As Exception MsgBox("exception caught first letter must be capital") End Try End Sub End Class In some situation ,users also have to create their own exception other than the defined ones. These exceptions are called user defined exception.
Users can catch the specific type of exception instead of
generic exception .
Our own exceptions must inherit from system . Exception class
Public Class Form7 Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click Dim a As Integer a = CInt(InputBox("enter your mark")) Try If (a > 100) Then Throw New my1("enter the mark less than 100") End If Catch obj As my1 MsgBox(obj.Message) End Try End Sub End Class Public Class my1 Inherits Exception Public Sub New(ByVal msg As String) MyBase.New(msg) End Sub End Class THANK YOU