Presentation is loading. Please wait.

Presentation is loading. Please wait.

Exceptions, handling exceptions & message boxes Year 11 Information Technology.

Similar presentations


Presentation on theme: "Exceptions, handling exceptions & message boxes Year 11 Information Technology."— Presentation transcript:

1 Exceptions, handling exceptions & message boxes Year 11 Information Technology

2 Main points of chapter 3... Variables Constants Calculations Parse method Arithmetic operations and order of Assignment operators (including short versions) Converting between numeric data types (implicit and explicit) Formatting data for display

3 Handling Exceptions Issues can occur when performing calculations. The parse methods, Integer.Parse and Decimal.Parse fails if the user enters nonnumeric data or leaves the text box blank. This causes an exception error to occur. Can determine program exceptions by using structured exception handling. These can be caught before they throw a run-time error. Catching exceptions as they happen and writing code to take care of the problems is called exception handling.

4 To try catch exceptions, enclose any statements that might cause an error in a Try/Catch Block. If an error occurs while the statements in the Try block are executing, program control transfers to the Catch block; if a Finally statement is included, the code in that section executes last, whether or not an exception occurred. (See page 138 for example). You can also define which error occurs (i.e. An error for incorrect input rather than calculation issues). Try/Catch Blocks

5 The Exception class Each exception is an instance of the Exception class. The properties of this class allow you to determine the code location of the error, the type of error, and the cause. The Message property contains a text message about the error, and the Source property contains the name of the object causing the error. The Stack-Trace property can identify the location in the code where the error occurred. You can include the text message associated with the type of exception by specifying the Message property of the Exception object, as declared by the variable you named on the Catch statement.

6 ExceptionCaused by FormatExceptionFailure of a numeric conversion, such as Integer.Parse or Decimal.Parse. Usually blank or nonnumeric data. InvalidCastExceptionFailure of a conversion operation. May be caused by loss of significant digits or an illegal conversion. ArithmeticExceptionA calculation error, such as division by zero or overflow of a variable. System.IO.EndOfStreamExceptionFailure of an input or output operation such as reading from a file. OutOfMemoryExceptionNot enough memory to create an object. ExceptionGeneric Common Exception Classes

7 Handling multiple exceptions To try and catch more than one type of exception, use multiple catch blocks. When an exception occurs, the Catch statements are checked in sequence. The first one with a matching exception type is used.

8 Displaying messages in message boxes To display a message when a user has entered invalid data, we use message boxes (a pop-up window). You use the Show method of the message box to display the message.

9 The MessageBox object – general forms More than one way to call the Show method. Very important that the arguments you supply exactly match one of the formats. You cannot reverse, transpose, or leave out any of the arguments. Read page 141-142 of your programming booklet for an explanation of how the difference sections of the statement effect the appearance of a message box. General structure: – MessageBox.Show ( TextMessage, TitleBarText, MessageBoxButtons, MessageBoxIcon) – E.g. MessageBox.Show ( “Enter numeric data”, “Invalid data entry”, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)

10 Activities What is an exception? List two things that may cause an exception. What does the term ‘exception handling’ refer to? Explain what Try/Catch blocks are and how they work. List three common exceptions and write what causes them. Write an assignment statement for ONE of these exceptions. Explain where Titlebar Text and MessageBoxIcon occur in a message box (use your programming booklets). Draw the example of the message box from Slide 9 as you think it would appear.


Download ppt "Exceptions, handling exceptions & message boxes Year 11 Information Technology."

Similar presentations


Ads by Google