Chapter 4
Chapter 4
Try
[ try 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
Types of Error
o Programming errors are generally broken down into three
types:
Design-time Error
RuntimeError , and
Logic errors.
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