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

Slides:



Advertisements
Similar presentations
Topics Introduction Types of Errors Exceptions Exception Handling
Advertisements

Exceptions Chapter Throwing and Catching Exceptions When a program runs into a problem that it cannot handle, it throws an exception. Exceptions.
Exceptions Don’t Frustrate Your User – Handle Errors KR – CS 1401 Spring 2005 Picture – sysprog.net.
Errors and Exceptions The objectives of this chapter are: To understand the exception handling mechanism defined in Java To explain the difference between.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 15: Exception Handling.
Try…Catch…Finally Blocks ( continued ) Generic catch clause –Omit argument list with the catch –Any exception thrown is handled by executing code within.
C# Programming: From Problem Analysis to Program Design1 Debugging and Handling Exceptions C# Programming: From Problem Analysis to Program Design 3 rd.
Chapter 16: Exception Handling C++ Programming: From Problem Analysis to Program Design, Fifth Edition.
Objectives In this chapter you will: Learn what an exception is Learn how to handle exceptions within a program See how a try / catch block is used to.
Chapter 11 Debugging and Handling Exceptions
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 16: Exception Handling.
Slides prepared by Rose Williams, Binghamton University ICS201 Exception Handling University of Hail College of Computer Science and Engineering Department.
Microsoft VB 2005: Reloaded, Advanced Chapter 5 Input Validation, Error Handling, and Exception Handling.
Lecture Roger Sutton CO331 Visual Programming 19: Simple file i/o Exceptions – Error handling 1.
Exceptions in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Exceptions CIS 304 Intermediate Java Programming for Business.
Exception Handling An Exception is an indication of a problem that occurs during a program’s execution. Exception handling enables the programmer to create.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 8 Exception Handling Sections 1-5, 7.
Exceptions. Many problems in code are handled when the code is compiled, but not all Some are impossible to catch before the program is run  Must run.
1 Exception Handling Introduction to Exception Handling Exception Handling in PLs –Ada –C++ –Java Sebesta Chapter 14.
Chapter 13 Exception Handling F Claiming Exceptions F Throwing Exceptions F Catching Exceptions F Rethrowing Exceptions  The finally Clause F Cautions.
Java Programming Exceptions Handling. Topics: Learn about exceptions Try code and catch Exceptions Use the Exception getMessage() method Throw and catch.
Chapter 12: Exception Handling
And other languages…. must remember to check return value OR, must pass label/exception handler to every function Caller Function return status Caller.
Understanding Events and Exceptions Lesson 3. Objective Domain Matrix Skills/ConceptsMTA Exam Objectives Understand events and event handling Understand.
1 Web-Enabled Decision Support Systems Objects and Procedures Don McLaughlin IE 423 Design of Decision Support Systems (304)
Chapter 7 Decision Making. Class 7: Decision Making Use the Boolean data type in decision-making statements Use If statements and Select Case statements.
PROGRAMMING IN VISUAL BASIC.NET VISUAL BASIC BUILDING BLOCKS Bilal Munir Mughal 1 Chapter-5.
Handling Exceptions in java. Exception handling blocks try { body-code } catch (exception-classname variable-name) { handler-code }
Java Programming: Guided Learning with Early Objects
Introduction to Exception Handling and Defensive Programming.
Visual Basic.NET BASICS Lesson 5 Exponentiation, Order of Operations, and Error Handling.
Chapter 14: Exception Handling. Objectives In this chapter, you will: – Learn what an exception is – Learn how to handle exceptions within a program –
Exception Handling Unit-6. Introduction An exception is a problem that arises during the execution of a program. An exception can occur for many different.
Exceptions in Java. Exceptions An exception is an object describing an unusual or erroneous situation Exceptions are thrown by a program, and may be caught.
BIO Java 1 Exception Handling Aborting program not always a good idea – can’t lose messages – E-commerce: must ensure correct handling of private.
Data Structures Using Java1 Chapter 2 Inheritance and Exception Handling.
Chapter 3 Variables, Constants and Calculations Programming In Visual Basic.NET.
Sheet 3 HANDLING EXCEPTIONS Advanced Programming using Java By Nora Alaqeel.
Exceptions, handling exceptions & message boxes Year 11 Information Technology.
Exceptions in C++. Exceptions  Exceptions provide a way to handle the errors generated by our programs by transferring control to functions called handlers.
Exceptions and Assertions Chapter 15 – CSCI 1302.
Chapter 15: Exception Handling C++ Programming: Program Design Including Data Structures, Fifth Edition.
Exceptions Handling Prepared by: Ligemm Mae del Castillo.
110 E-1 Variables, Constants and Calculations(2) Chapter 3: Operations on variables, scope of a variable, formatting data Doing Arithmetic.
(c) University of Washington10-1 CSC 143 Java Errors and Exceptions Reading: Ch. 15.
IMS 3253: Validation and Errors 1 Dr. Lawrence West, MIS Dept., University of Central Florida Topics Validation and Error Handling Validation.
Lecture10 Exception Handling Jaeki Song. Introduction Categories of errors –Compilation error The rules of language have not been followed –Runtime error.
C# Exceptions 1 CNS 3260 C#.NET Software Development.
And other languages…. must remember to check return value OR, must pass label/exception handler to every function Caller Function return status Caller.
Exception and Exception Handling. Exception An abnormal event that is likely to happen during program is execution Computer could run out of memory Calling.
Exception Handling. VB.NET has an inbuilt class that deals with errors. The Class is called Exception. When an exception error is found, an Exception.
ECE122 L23: Exceptions December 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 24 Exceptions.
1 Handling Errors and Exceptions Chapter 6. 2 Objectives You will be able to: 1. Use the try, catch, and finally statements to handle exceptions. 2. Raise.
Garbage Collection It Is A Way To Destroy The Unused Objects. To do so, we were using free() function in C language and delete() in C++. But, in java it.
Chapter 6 Controlling Program Flow with Looping Structures.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Enhanced Car Payment Calculator Application Introducing Exception Handling.
Lecture 11 Dr. Eng. Ibrahim El-Nahry Exception Handling.
Lec.11 (Chapter 11) Exception Jiang (Jen) ZHENG July 13 th, 2005.
IS 350 Decision-making.
Chapter 14: Exception Handling
CNS 3260 C# .NET Software Development
Topics Introduction to File Input and Output
Part A – Doing Your Own Input Validation with Simple VB Tools
Exception Handling and Reading / Writing Files
Part B – Structured Exception Handling
Debugging and Handling Exceptions
Topics Introduction to File Input and Output
Chapter 11: Exception Handling
Presentation transcript:

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

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

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