Debugging and Handling Exceptions

Slides:



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

C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 15: Exception Handling.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
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
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 16: Exception Handling.
Exceptions Briana B. Morrison CSE 1302C Spring 2010.
1 Lecture 11 Interfaces and Exception Handling from Chapters 9 and 10.
Microsoft VB 2005: Reloaded, Advanced Chapter 5 Input Validation, Error Handling, and Exception Handling.
Chapter 12: Advanced Topics: Exception Handling Visual Basic.NET Programming: From Problem Analysis to Program Design.
Advanced Java Course Exception Handling. Throwables Class Throwable has two subclasses: –Error So bad that you never even think about trying to catch.
UNIT 3 TEMPLATE AND EXCEPTION HANDLING. Introduction  Program errors are also referred to as program bugs.  A C program may have one or more of four.
WEEK EXCEPTION HANDLING. Syntax Errors Syntax errors, also known as parsing errors, are perhaps the most common kind of complaint you get while.
PROGRAMMING IN VISUAL BASIC.NET VISUAL BASIC BUILDING BLOCKS Bilal Munir Mughal 1 Chapter-5.
10/20/2015Assoc. Prof. Stoyan Bonev1 COS240 O-O Languages AUBG, COS dept Lecture 37 Title: C# vs. Java Exception Handling Reference: COS240 Syllabus.
Introduction to Exception Handling and Defensive Programming.
Chapter 14: Exception Handling. Objectives In this chapter, you will: – Learn what an exception is – Learn how to handle exceptions within a program –
Controlling Execution Programming Right from the Start with Visual Basic.NET 1/e 8.
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.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Exceptions and Assertions Chapter 15 – CSCI 1302.
Exception Handling in Java Topics: Introduction Errors and Error handling Exceptions Types of Exceptions Coding Exceptions Summary.
Chapter 15: Exception Handling C++ Programming: Program Design Including Data Structures, Fifth Edition.
Error Handling Tonga Institute of Higher Education.
Exception and Exception Handling. Exception An abnormal event that is likely to happen during program is execution Computer could run out of memory Calling.
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.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Enhanced Car Payment Calculator Application Introducing Exception Handling.
Lec.11 (Chapter 11) Exception Jiang (Jen) ZHENG July 13 th, 2005.
Eighth Lecture Exception Handling in Java
16 Exception Handling.
Exceptions.
Debugging and Handling Exceptions
Programming in visual basic .net Visual Basic Building Blocks
ME 142 Engineering Computation I
Handling Exceptionally Sticky Problems
Reference: COS240 Syllabus
Why exception handling in C++?
Computer Programming I
Chapter 14: Exception Handling
Exceptions 10-Nov-18.
Debugging and Handling Exceptions
5.01 Understand Different Types of Programming Errors
Exception Handling Chapter 9.
Topics Introduction to File Input and Output
Exceptions & Error Handling
Exception Handling.
Exceptions Problems in a Java program may cause exceptions or errors representing unusual or invalid processing. An exception is an object that defines.
Exception Handling Chapter 9 Edited by JJ.
Part B – Structured Exception Handling
Exceptions.
Programming in C# CHAPTER - 7
Exception Handling Imran Rashid CTO at ManiWeber Technologies.
Problems Debugging is fine and dandy, but remember we divided problems into compile-time problems and runtime problems? Debugging only copes with the former.
Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output Prof. Salim Arfaoui.
Exceptions 25-Apr-19.
Exceptions 22-Apr-19.
Introduction to Programming
Exception and Event Handling
Handling Exceptionally Sticky Problems
Topics Introduction to File Input and Output
Chapter 11: Exception Handling
Exceptions 5-Jul-19.
CMSC 202 Exceptions 2nd Lecture.
CMSC 202 Exceptions.
Exception Handling.
Presentation transcript:

Debugging and Handling Exceptions Chapter 11 Presented by Rodion Karmazin

!!Beforehand Warning!! None of the content in this PowerPoint belongs to me. All rights for the content go to their respective owners.

Quick Summary What you shall learn from this HUGE chapter are the “Try”, “Catch”, and “Finally” methods. Also you shall find out what debugging really is. I will also do my best to explain something about Exceptions

Error message does not always state the correct problem Errors Visual Studio IDE reports errors as soon as it is able to detect a problem Syntax errors Language rule violation Error message does not always state the correct problem Quick info Figure 11-1 Syntax error – extraneous semicolon

Run-Time Errors Just because your program reports no syntax errors does not necessarily mean it is running correctly One form of run-time error is a logic error Program runs, but produces incorrect results May be off-by-one in a loop Sometime users enter incorrect values Finding the problem can be challenging

“Exceptions” Some circumstances are beyond programmer’s control You have assumed nothing unusual would occur Have probably experienced unhandled exceptions being thrown While you browsed Web pages Unless provisions are made for handling exceptions, your program may crash or produce erroneous results Unhandled exception

“Exceptions” example Dialog box asks you whether you want to have an error report sent to Microsoft Figure 11-9 Microsoft error reporting

“Exceptions” example cont. Message displayed when you are creating console application and unhandled exception occurs Figure 11-11 Unhandled exception in a console application

“Unhandled Exceptions” Selecting Debug>Start to run application in Visual Studio Yellow arrow marks the error erroneous code highlighted Figure 11-12 Unhandled exception thrown – dividing by zero

Raising an “Exception” Error encountered – no recovery Raise or throw an exception Execution halts in the current method and the “Common Language Runtime” or “CLR” attempts to locate an “exception handler” Exception handler: block of code to be executed when a certain type of error occurs If no exception handler is found in current method, exception is thrown back to the calling method

Exception-Handling Techniques If event creates a problem frequently, best to use conditional expressions to catch and fix problem Execution is slowed down when CLR has to halt a method and find an appropriate event handler Exception-handling techniques are for serious errors that occur infrequently Exceptions classes integrated within the FCL Used with the try…catch…finally program constructs .NET Framework Class Library

Try…Catch…Finally Blocks Code that may create a problem is placed in the try block Code to deal with the problem (the exception handler) is placed in catch blocks Catch clause Code to be executed whether an exception is thrown or not is placed in the finally block

catch [ (ExceptionClassName exceptionIdentifier) ] try { // Statements } catch [ (ExceptionClassName exceptionIdentifier) ] // Exception handler statements : // [additional catch clauses] [ finally } ] Notice square brackets indicate optional entry One catch clause required finally clause optional

Try…Catch…Finally Blocks (continued) Generic catch clause Omit argument list with the catch Any exception thrown is handled by executing code within that catch block Control is never returned into the try block after an exception is thrown Using a try…catch block can keep the program from terminating abnormally

Use of Generic Catch Clause This example uses a generic catch block Figure 11-14 Generic catch block handles the exception

// calculate average grade for a class int totalGrades = 0; int grade = 0; int sumGrades = 0; String data = ""; Console.WriteLine("Please enter a grade [## to end]"); while (true) { try { data = Console.ReadLine(); grade = int.Parse(data); sumGrades += grade; totalGrades++; } catch (Exception e) if (data == "##") break; Console.WriteLine("Invalid data - try again"); Console.WriteLine("Please enter a grade [## to end]"); if (totalGrades > 0) Console.WriteLine("Average grade is {0:F2}", sumGrades / totalGrades); else Console.WriteLine("Average = 0 (No data was supplied)");

Comments, complaints, suggestions are also inquired for at this time! Any Questions? Comments, complaints, suggestions are also inquired for at this time! !!!!!!!! READ THE BOOK

Assignments (5 Pointer) (10 Pointer) Make a program that requests a phone number. If a knucklehead runs the program and types in a letter, display an error message. “Try” and “Catch” are required for this. (10 Pointer) Try to make out whatever is happening on slide 16 and make a program that asks for the number of scores and has the user put in the scores. If there is an error in the input, address that error immediately with a message box or Console.Writeline();