Sunteți pe pagina 1din 22

Web Engineering‐II 

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.

• As you might expect due to the language independence of


the framework you can also write error handling in VB.NET.

• In an application, one challenge faced by developers is


dealing with the errors that pop up or return wrong result.

By: Adnan
3
Amin(Lecturer/Programmer)
Types of Errors 

Errors  Exceptions 

Compile  Design  Runtime 


Time  Time  Errors 

Logical Errors 
Syntax Errors 

• Application errors come in three varieties:

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.

• This type of error can be extremely difficult to track down


and fix because it doesn’t stop the execution of the
program.

• In visual basic.net, two steps you can take to prevent


errors are using design time error capturing..
o Option Explicit
o Option Strict
By: Adnan
7
Amin(Lecturer/Programmer)
Preventing Runtime Errors 
• If possible you should capture errors as early as
possible as fewer will then make it through to the
runtime environment.

• VB.Net offers the Option Strict and Option Explicit


statements to prevent errors at design time.\

• In ASP.NET Option Explicit is set to on by default.

By: Adnan
8
Amin(Lecturer/Programmer)
1. Option Explicit                    Preventing Runtime Errors Cont. 

• Using Option Explicit will raise a syntax error it you


attempt to use a variable before declaring it,

• If for any reason you did need to reset (On or Off),


the syntax is:

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. 

• Using Option Strict will raise a syntax error if you attempt an


implicit data type conversion that would result in a loss of
data.
• Option Strict error on any attempt to use an undeclared
variable.

• The following example demonstrates the use of Option Strict:


Option Strict  On 
 
 Dim A as  Integer 
 Dim B as  Double 
  
  B = 5.4212  This  line  will 
 A= B  cause  an  error  at 
 Msgbox “ the value of A is: “ & A 
By: Adnan
compile time. 
10
Amin(Lecturer/Programmer)
Important 
• Both Option Explicit and Option Strict
must come before any other code in the
module in which they appear.

By: Adnan
11
Amin(Lecturer/Programmer)
Types of Error Handing 
Structured Error  Unstructured Error 
Handling  Handling 

• There are two types of Error Handing.


1. Structured Error handling
2. Unstructured Error 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.

• Exception handler allows you to easily


locate the error.

• Exception handler prevents the program


from automatically terminating.

• If exception arise in a program and it can


easily handle on their own choice.
By: Adnan
17
Amin(Lecturer/Programmer)
Structured Error Handling Syntax 
• Try 
o [Try statements block] 

• 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)

S-ar putea să vă placă și