IS437: Fall 2004 Instructor: Dr. Boris Jukic Exceptions and Exception Handling Message Boxes.
-
Upload
theodore-quinn -
Category
Documents
-
view
212 -
download
0
Transcript of IS437: Fall 2004 Instructor: Dr. Boris Jukic Exceptions and Exception Handling Message Boxes.
IS437: Fall 2004
Instructor: Dr. Boris Jukic
Exceptions and Exception Handling
Message Boxes
•Exceptions: objects created by the system when an error event takes place during the code execution
•Three general types of Errors:
•User errors: inconsistent data type of input, for example
•Resource Failures: out of memory, for example
•Failures of programming logic
• Examples of exception messages: file not found, division by zero, disk full, disk not ready, out of memory, …
• Unhandled exceptions lead to abrupt termination of programs without warnings and explanations.
•
Exceptions
•Exception handling means interpreting and reacting to the exceptions created by errors.
•The goals of exception handling are to anticipate potential problems and provide opportunities to address the problems when they occur.
•Structured Exception handling uses a control structure to create exception handling mechanisms that differentiate between different types of errors
•Try...Catch...Finally control structure tests a piece of code and reacts differently based on the different types of errors
Exception Handling
Try/Catch Blocks
…
…
Try
statements that may cause error
Catch
what to do if an error occurs
[Finally
do this regardless of whether error occurs or not]
End Try
…
…
A Simple Example
Private Sub Button1_Click(…) Handles Button1.Click
Dim x, y, z As Short x = 1000 y = 1000 Try z = x * y Catch ‘An exception will be thrown if x value is outside of range for Short MessageBox.Show("Error has occurred", "Error") End Try
End Sub
Exception Classes and Properties
Exceptions are objects belonging to different classes
General Exception class Use title “Exception”
Specific Exception classes: FormatException, ArithmeticException, etc.
Exception objects have several properties: Message property holds a string with a text message
about the nature of the error that caused the exception
Catch … As
Exception
ArithmeticException
IOException DataException
OverflowException
DivisonByZeroException
DiskNotReadyException
…
FileNotFoundException
MissingPrimaryKeyException
…
…
…
General Exception:
Catch ex as Exception
‘create ex as an object of Exception class
Specific Exceptions:
“Catch …As” example
Private Sub Button1_Click(…) Handles Button1.Click
Dim x, y, z As Short x = 1000 y = 1000 Try z = x * y Catch ex As Exception MessageBox.Show(ex.Message, "Error") End Try End Sub
Message property of exException. ex is an instance ofOverflowException sub class ofArithmenticException class.
Multiple Catch Clauses
Try
‘the code of the procedure goes here
Catch err As ArithmeticException ‘more specific exception
‘what to do
Catch err As Exception ‘most general exception
‘what to do
End Try
•Sequential search for error
•Only the block that first catches an error executes.
•Always list the most specific exception statement fist
Incorrect Catch Clause: an Example
Try
‘the code of the procedure goes here
Catch err As Exception ‘most general exception
‘what to do
Catch err As ArithmeticException ‘more specific exception
‘what to do
End Try
Catch err as ArithmeticException can never execute in this example.
“Catch .. As .. When” Clause: an Example
Try
varAvailableSeats = varAuditoriumSeats - varNumberOfGuests
Catch ex As Exception When varAuditoriumSeats = 0
MessageBox.Show("Auditorium lacks chairs!")
Catch ex As Exception When varAvailableSeats < 0
MessageBox.Show("There are no more available seats.")
Catch ex As Exception
‘all other exceptions
Finally MessageBox.Show("Thank you for your interest in our concert.")
End Try
With when clause, the programmer can define what constitutes an exception
Message Boxes
Dialog Boxes displaying message to the users (often as a result of exceptions)
Invoked by a MessageBox.Show() method MessageBox.Show is a so called
“overloaded method” Accepts many different combinations of
arguments:
MessageBox.Show Method Arguments
“Text to be displayed” in the body of the message box “Text to be displayed” in the title of the message box Type of a button combination: MessageBoxButton
argument Type of an icon displayed: MessageBoxIcon argument
Message Box Button Combinations
MessageBoxButton.OK Displays the OK button MessageBoxButton.OKCancel Displays the OK and
Cancel buttons MessageBoxButton.AbortRetryIgnore Displays Abort,
Retry, and Ignore buttons MessageBoxButton.YesNoCancel Displays the Yes, No,
and Cancel buttons MessageBoxButton.YesNo Displays the Yes and No
buttons MessageBoxButton.RetryCancel Displays the Retry and
Cancel buttons
Message Box Icons
MessageBoxIcon.Exclamation MessageBoxIcon.Question MessageBoxIcon.Error MessageBoxIcon.Information