Presentation is loading. Please wait.

Presentation is loading. Please wait.

IS437: Fall 2004 Instructor: Dr. Boris Jukic Exceptions and Exception Handling Message Boxes.

Similar presentations


Presentation on theme: "IS437: Fall 2004 Instructor: Dr. Boris Jukic Exceptions and Exception Handling Message Boxes."— Presentation transcript:

1 IS437: Fall 2004 Instructor: Dr. Boris Jukic Exceptions and Exception Handling Message Boxes

2 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

3 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

4 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 …

5 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

6 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

7 Catch … As Exception Arithmetic Exception IOExceptionDataException Overflow Exception DivisonBy ZeroExcep tion DiskNotReady Exception … FileNotFound Exception MissingPrimaryK eyException … … … General Exception: Catch ex as Exception ‘create ex as an object of Exception class Specific Exceptions:

8 “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 ex Exception. ex is an instance of OverflowException sub class of ArithmenticException class.

9 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

10 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.

11 “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

12 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:

13 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

14 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

15 Message Box Icons MessageBoxIcon.Exclamation MessageBoxIcon.Question MessageBoxIcon.Error MessageBoxIcon.Information


Download ppt "IS437: Fall 2004 Instructor: Dr. Boris Jukic Exceptions and Exception Handling Message Boxes."

Similar presentations


Ads by Google