0% found this document useful (0 votes)
68 views24 pages

Chapter 4

The error is that x is declared as an Integer but assigned a decimal value of 10.5. When x is multiplied by the integer y, it performs an integer multiplication which discards the decimal portion of 10.5, resulting in 10 instead of the expected 31.5. This is a logic error - the code is syntactically valid but does not perform the calculation as intended due to a flaw in the logic.

Uploaded by

Game Teka
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
68 views24 pages

Chapter 4

The error is that x is declared as an Integer but assigned a decimal value of 10.5. When x is multiplied by the integer y, it performs an integer multiplication which discards the decimal portion of 10.5, resulting in 10 instead of the expected 31.5. This is a logic error - the code is syntactically valid but does not perform the calculation as intended due to a flaw in the logic.

Uploaded by

Game Teka
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 24

Mettu University

Event driven programming ITec3053

Chapter 4- Exception Handling


Outline

o Introducing Exception Handling


o Structured Exception Handling
o Implementing Exception Handling
Introducing Exception Handling

o An exception: is a problem that arises during the


execution of a program.
o An exception is a response to an exceptional circumstance
that arises while a program is running, such as an attempt
to divide by zero.
o Exception is Run time error
o It is not known at the compile time
o Examples of exceptions
– Divide by zero
– Accessing non existing portion of memory
– running program out of memory
Exception Classes in vb.net

o In the .NET Framework, exceptions are represented by classes.


o The exception classes in .NET Framework are mainly directly or
indirectly derived from the System.Exception class.
o System.Exception is the base class for all exceptions
o Exception class are the System.ApplicationException and
System.SystemException classes.
The System.ApplicationException 
o class supports exceptions generated by application programs.
o can be user defined exceptions thrown by the application.
o So the exceptions defined by the programmers should derive from this
class.
The System.SystemException 
o class is the base class for all predefined system exceptionSystem
exceptions are common exceptions thrown by the CLR..
Exception class
Exception Class Description

System.IndexOutOfRangeException Handles errors generated when a


method refers to an array index out of
range.
System.ArrayTypeMismatchException Handles errors generated when type is
mismatched with the array type.
System.NullReferenceException Handles errors generated from
deferencing a null object.
System.InvalidCastException Handles errors generated during
typecasting.
System.OutOfMemoryException Handles errors generated from
insufficient free memory.

System.DivideByZeroException Handles errors generated from dividing a


dividend with zero.
NullReferenceException
oA NullReferenceException exception is thrown when you try
to access a member on a type whose value is null.
oThat means the reference to an Object which is not
initialized.
Difference between Exception and Error
oExceptions are related to the application and an Error is
related to the environment in which the application is
running.
Structured Exception Handling

o VB.Net provides a structured solution to the exception


handling problems in the form of try and catch blocks.
o Using these blocks the core program statements are
separated from the error-handling statements.
o These error handling blocks are implemented using
the Try, Catch and Finally keywords. 
o It is significantly faster in large applications than
unstructured exception handling.
Syntax Try …. Catch

Try
[ try statements ]

Catch ExceptionVariable As ExceptionType


[ catch statements ]

‘Additional Catch block


Finally
[ finally statements ]

End Try
Try Block
Try Block:
oMeans “ Try to execute this code “
oEncloses code that might throw an exception and the code
that should not execute if an exception occurs.
oCode that may invoke exception placed in Try block.
oBegins with keyword Try.
oEnds with keyword End Try.
oThere must be at least one Catch block and/or Finally block
immediately after the Try block
Catch Block
Catch Block:
oMeans "Catch any errors here”
oCatches and handles an exception.
oBegins with keyword Catch.
oSpecifies an identifier and exception type
oExample: Catch e As Exception
oExecutes when exception of proper type matches
Finally block
Finally block:
o Optional in a Try statement
o Placed after the last Catch block (if there is one)
o Executes whether or not an exception is thrown in the
corresponding Try block or any of its corresponding
Catch blocks
o Generally we use it to close an opened file or connection
Throw block
oThrow block:
⇨You should throw exceptions only when an unexpected or
invalid activity occurs that prevents a method from
completing its normal function.
⇨You can throw any type of Throwable object using the
keyword throw.
Termination Model of Exception Handling

o When an exception occurs:


– Try block terminates immediately
– Program control transfers to first matching Catch block (other
Catch block are ignored)
o After exception is handled:
– Termination model of exception handling
o Program control does not return to the throw point because
the Try block has expired
o Flow of control proceeds to the first statement after the last
Catch block
– Resumption model of exception handling
o Program control resumes just after throw point
Example:1 divide a number by zero .
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles MyBase.Load
Try
Dim i As Integer
Dim resultValue As Integer
i = 100
resultValue = i / 0 Output
MsgBox("The result is " & resultValue) When you execute this program you will
Catch ex As Exception "Exception catch here .." first and then
MsgBox("Exception catch here ..") "Finally block executed " . That is when
Finally you execute this code , an exception
MsgBox("Finally block executed ") happen and it will go to catch block and
End Try then it will go to finally block.
End Sub
End Class
Example2: DivideByZeroTest

Dim n1, n2, r As Integer


Try
n1 = Textbox1.Text
n2 = Textbox2.Text
r = n1 / n2
MessageBox.Show(r)
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
DivideByZeroTest
Error handling and debugging in vb.net

Types of Error
o Programming errors are generally broken down into three
types:
Design-time Error
RuntimeError , and
Logic errors.
Types of Error

1.A Design-time error:


ois also known as a syntax error.
oOccur when you mistype a command.
oThese occur when the environment you're programming in
doesn't understand your code.
oThese are easy to track down in VB.NET, because you get a
blue wiggly line pointing them out.
oIf you try to run the program, you'll get a dialogue box
popping up telling you that there were Build errors.
oYou cannot run a visual basic program until all syntax errors
have been corrected.
Types of Error…

2. Runtime errors:
oare usually beyond your program’s control
oAs their name suggests, these errors occur when the
program is running.
oThey happen when your program tries to do something it
shouldn't be doing.
oAn example is trying to access a file that doesn't exist, when
a variable takes an expected value(divide by zero)
o Runtime errors usually cause your program to crash.
oYou should write code to trap runtime errors.
Types of Error…

3. Logic errors:
o also occur when the program is running.
oThey happen when your code doesn't quite behave the way
you thought it would.
oMost difficult to find.
oWith logic errors the program will always run, but will
produce incorrect or unexpected results.
oa logic error is a human error – a programming mistake that
makes the program code produce the wrong results.
oThe visual basic debugger is an aid in detecting logic errors.
Some ways to minimize errors:
oDesign your application carefully.
- more design time means less debugging time.
oUse comments where applicable to help you remember what
you were trying to do.
oUse consistent and meaningful naming conventions for your
variables, objects and procedures.
Runtime errors in VB .NET

Runtime errors:
oerrors occur when the program is running.
oRuntime errors are the ones that crash your program.
oA simple way to crash a program is to divided by zero:
Dim Num1 As Integer
Dim Num2 As Integer
Num1 = 10
Num2 = 0
TextBox1.Text = CInt(Num1 / Num2)
oThe CInt( ) part means Convert to an Integer. We're just
making sure to convert the answer to the sum into a number.
But run your program and test it out. Click your button and
see what happens.
Logic Errors in VB .NET

o Add a button to the form and try this code as an example of


a logic error:
Dim x As Integer
Dim y As Integer
Dim answer As Integer
x = 10.5
y=3
answer = x * y
TextBox1.Text = answer
What is the error??

You might also like