Professional Documents
Culture Documents
Lesson - 14 Error Handling
Lesson - 14 Error Handling
Lesson - 14 Error Handling
Using ASP.net
BY
Adnan Amin
Lecturer/ Software Programmer
Information & Communication Technology (ICT Dept)
www.geoamins.com
Overview
• What’s Error/Exception Handling?
• Types of Errors
o Syntax Error
o Runtime time
• Preventing Runtime Errors
o 1. Option Explicit
o 2. Option Strict
• Types of Error Handing
o Structured
o Unstructured
• Exception
• Exception Handlers (Try…Catch….Finally)
• Advantages of Exception Handler
By: Adnan
Amin(Lecturer/Programmer)
2
What’s Error/Exception Handling?
• The .NET Framework’s Common Language Runtime (CLR)
implements exception handling as one of its fundamental
features.
By: Adnan
3
Amin(Lecturer/Programmer)
Types of Errors
Errors Exceptions
Logical Errors
Syntax Errors
By: Adnan
4
Amin(Lecturer/Programmer)
1. Syntax Errors
• These include misspelled or missing
1. Keywords
2. Mistakes relating to the syntax
• Syntax errors are typically flagged by
the IDE and may be easily fixed at
design time.
If condition then End If is the correct syntax
body of if‐statement according to vb.net language.
End
For i=0 Too 5 For i=0 to 5 is the correct syntax
body of for‐loop according to vb.net language.
Next i
By: Adnan
5
Amin(Lecturer/Programmer)
2. Runtime Errors
• These are errors that appear once your code is compiled
and running. (During execution of a program if any error
arise )
• Runtime errors are caused by code that appears correct
to the compiler, but that cannot run with certain values.
For Example, For Example,
Dim a as Integer = 10 Code that divides one
Msgbox “Result” & a/0 integer variable by another
will appear to be correct to a
compiler, but it will cause a
runtime error it the second
variable is zero.
By: Adnan
6
Amin(Lecturer/Programmer)
3. Logical Errors
• These are errors in which a program that works correctly
under some (or most) circumstances gives unexpected or
unwanted result based on certain input values.
By: Adnan
8
Amin(Lecturer/Programmer)
1. Option Explicit Preventing Runtime Errors Cont.
Option Explicit On
It can help you greatly
Dim sum as Double if you have a habit of
misspelling variable
Sam = 10 + 20
Msgbox “Sum is: “ & sum names.
By: Adnan
9
Amin(Lecturer/Programmer)
1. Option Strict Preventing Runtime Errors Cont.
By: Adnan
11
Amin(Lecturer/Programmer)
Types of Error Handing
Structured Error Unstructured Error
Handling Handling
By: Adnan
12
Amin(Lecturer/Programmer)
1. Unstructured Error Handling
• This is the method of using a single error
handler within a method that catches all
exceptions.
• It can’t use to catch a single particular type of
error.
• For Example;
o On Error GoTo line [or] label
o On Error Resume Next
By: Adnan
13
Amin(Lecturer/Programmer)
Using the On Error Statement
• The On Error statement transfers execution to the section
of code indicated by the ErrorHandler label.
• Once the error-handling code has executed, the Resume
next statement causes execution to return to the statement
immediately to the lines that caused the error.
• The Exit Sub statement appearing just to prevents the
error-handler from running if no error occurs.
Sub ExampleError ( )
On Error GoTo ErrorHandler
‘Code that might cause an error
Exit Sub
ErrorHandler:
‘Error handling code
Resume Next
End Sub
By: Adnan
14
Amin(Lecturer/Programmer)
Exception
• An exception is an abnormal condition that arises in
code sequence at Run time.
• Exception is a runtime error.
• The Try…Catch….Finally for of exception handling,
also known as structures exception handling.
Exception Handler / Structures Exception Handling
There are the following exception handlers;
1. Try
2. Catch
3. Finally
By: Adnan
15
Amin(Lecturer/Programmer)
Exception Handler Cont.
• Try Block
o In Try Black, we keep such code which may arise an
exception.
• Catch Block
o After Try Block immediately used catch block (one or
more) which take / catch the arise exception. You can
define Catch blocks for as many specific errors as you
wish to handle.
• Finally Block
o In this Block, we type such code which will executes
in both cases, exception arise or not.
o The finally block can be used for any code you want
to run after the exception handling code. Such as
clean up code, close connection etc.
By: Adnan
16
Amin(Lecturer/Programmer)
Advantages of Exception Handler
• Exception handler allows you to easily fix
the errors in your coding.
• Catch ex As Exception
o [Catch statements block]
• Finally
o [Finally statements block]
• End Try
By: Adnan
18
Amin(Lecturer/Programmer)
Using Multiple Catch Block
• Try
o [code that may cause an exception]
• Catch ex As ArgumentException
o [code that may cause an exception]
• Catch ex2 As HttpException
o [code that may cause an exception]
• Catch ex3 As Exception
o [code that may cause an exception]
• Finally
o [Finally statements block]
• End Try
By: Adnan
19
Amin(Lecturer/Programmer)
Example: Exception Handling
• Dim a, b As Integer
• Dim c As Integer
• Try
• a=1
• b=0
• c=a/b
• Catch ex As Exception
• MsgBox(ex.Message)
• Exit Try
• Finally
• MsgBox("Everything has closed")
• End Try
By: Adnan
20
Amin(Lecturer/Programmer)
Predefined exception types provided
by the .NET Runtime
Exception type Description Example
Exception Base class for all See Previous
exceptions Slide
? ? ?
? ? ?
Assignment: Due Date: 05.12.10
Prepared List of Predefined exceptions types provided by the .Net
Runtime.
By: Adnan
21
Amin(Lecturer/Programmer)
Thank You!
• References
o Microsoft ASP.net (Step by Step) Written By G.Andrew Duthie
o http://www.DotNetJhon.com/PrintFriend.aspx?articleid=42
o http://www.geoamins.com
o Adnan’s Notes (Personal Notes about OOPs)
By: Adnan
22
Amin(Lecturer/Programmer)