exceptions, handling exceptions & message boxes year 11 information technology

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

Upload: david-byrd

Post on 21-Jan-2016

218 views

Category:

Documents


0 download

TRANSCRIPT

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

Exceptions, handling exceptions & message boxes

Year 11 Information Technology

Page 2: Exceptions, handling exceptions & message boxes Year 11 Information Technology

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

Page 3: Exceptions, handling exceptions & message boxes Year 11 Information Technology

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.

Page 4: Exceptions, handling exceptions & message boxes Year 11 Information Technology

• 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

Page 5: Exceptions, handling exceptions & message boxes Year 11 Information Technology

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.

Page 6: Exceptions, handling exceptions & message boxes Year 11 Information Technology

Exception Caused by

FormatException Failure of a numeric conversion, such as Integer.Parse or Decimal.Parse. Usually blank or nonnumeric data.

InvalidCastException Failure of a conversion operation. May be caused by loss of significant digits or an illegal conversion.

ArithmeticException A calculation error, such as division by zero or overflow of a variable.

System.IO.EndOfStreamException Failure of an input or output operation such as reading from a file.

OutOfMemoryException Not enough memory to create an object.

Exception Generic

Common Exception Classes

Page 7: Exceptions, handling exceptions & message boxes Year 11 Information Technology

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.

Page 8: Exceptions, handling exceptions & message boxes Year 11 Information Technology

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.

Page 9: Exceptions, handling exceptions & message boxes Year 11 Information Technology

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)

Page 10: Exceptions, handling exceptions & message boxes Year 11 Information Technology

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.