VB Exceptions - PowerPoint PPT Presentation

1 / 14
About This Presentation
Title:

VB Exceptions

Description:

attempts to use null object. If not caught, these exceptions stop program execution ... This exception can be caught and an error message posted. Exception Example ... – PowerPoint PPT presentation

Number of Views:23
Avg rating:3.0/5.0
Slides: 15
Provided by: davidb195
Category:
Tags: an | error | exceptions | how | is | message | not | null | object | or | stop | to

less

Transcript and Presenter's Notes

Title: VB Exceptions


1
VB Exceptions
  • The VB Exception mechanism handles errors and
    exceptional circumstances with minimal logic in
    your code
  • Exceptions allow you to write code as though it
    executes without problems and uses separate
    exception handlers to deal with errors and
    exceptions that may arise during execution

2
Exceptions
  • Exceptions are thrown by attempts to perform
    illegal operations, such as
  • division by 0
  • conversion errors
  • attempts to use null object
  • If not caught, these exceptions stop program
    execution

3
Exception Handling
  • Exceptions can be caught and processed without
    ending the program
  • Multiple exceptions can be thrown and multiple
    exceptions can be caught
  • All exceptions can be caught at the same place in
    a procedure, making the rest of the code simpler

4
Exception Handling (Cont'd)
  • Exceptions are passed up the execution chain
    until they are either caught by your code or by
    VB
  • You may catch an exception at any level before VB
    does

5
Exception Handling (Cont'd)
  • Say subroutine A calls subroutine B which calls
    subroutine C. If subroutine C throws an
    exception
  • you may catch the exception in C
  • you may catch the exception in B
  • you may catch the exception in A
  • you may leave it to VB to catch the exception,
    which ends the program

6
Exception Example
  • This form asks the user to enter a number and
    dies if the user enters a non-number

7
Exception Example (Cont'd)
  • If the exception is caught instead, it can be
    handled by the program and the user may try again

8
Exception Handling
  • Exceptions can be caught and handled using the
    constructTry statementsCatch exception As
    exType statementsFinally statementsEnd
    Try

9
Exception Handling Construct
  • Try clause contains code to be executed
  • Catch clause contains the name of the exception
    to be caught and the code to deal with it
  • Finally clause contains code to be executed
    whether an exception was thrown or not

10
Exception Handling (Cont'd)
  • When an exception is thrown execution of the
    program is immediately transferred to the first
    catch block that can handle that exception

11
Exception Handling (Cont'd)
  • Public Sub A() Try 1st Line 2nd Line
    3rd Line Catch e As Exception
  • If 1st Line throws an exception then 2nd and 3rd
    Line are not executed execution moves to the
    Catch block

12
Exception Properties
  • Message a user-friendly message about the
    exception that was thrown
  • ToString a more detailed description of the
    exception that was thrown including its type and
    the Message
  • Not very user friendly but useful to the
    programmer to see exactly what exception was
    thrown

13
Exception Example
  • The number form tries to convert the contents of
    the textbox into a Decimal value
  • Non numbers cause the conversion to throw an
    InvalidCastException
  • This exception can be caught and an error message
    posted

14
Exception Example
  • Dim n As Decimal, flag As Boolean TrueTry
    n CDec(tbMyNumber.Text)Catch ex As
    InvalidCastException flag False
    MsgBox(ex.Message)Finally If flag Then
    lblResult.Text "Valid Number" Else
    lblResult.Text "Invalid Number" End IfEnd
    Try
Write a Comment
User Comments (0)
About PowerShow.com