Exceptions Programming 1 C# Programming. Rob Miles

Similar documents
Exceptions. Exceptions. Exceptional Circumstances 11/25/2013

Le L c e t c ur u e e 5 To T p o i p c i s c t o o b e b e co c v o e v r e ed e Exception Handling

Advanced Flow of Control -- Introduction

Traditional Error Handling

CMSC131. Exceptions and Exception Handling. When things go "wrong" in a program, what should happen.

Exceptions. Errors and Exceptions. Dealing with exceptions. What to do about errors and exceptions

CS112 Lecture: Exceptions. Objectives: 1. Introduce the concepts of program robustness and reliability 2. Introduce exceptions

Exceptions in Java

Exceptions. CSC207 Winter 2017

Exceptions and Design

16-Dec-10. Consider the following method:

Programming II (CS300)

Object Oriented Programming

ECE 122. Engineering Problem Solving with Java

CSC207H: Software Design. Exceptions. CSC207 Winter 2018

Programming II (CS300)

Error Handling in C++

Expressions and Casting. Data Manipulation. Simple Program 11/5/2013

CS112 Lecture: Exceptions and Assertions

Debugging and Handling Exceptions

Name: Checked: Preparation: Write the output of DeckOfCards.java to a text file Submit through Blackboard by 8:00am the morning of Lab.

Expressions and Casting

Exceptions - Example. Exceptions - Example

The University of Melbourne Department of Computer Science and Software Engineering Software Design Semester 2, 2003

Exception Handling. Sometimes when the computer tries to execute a statement something goes wrong:

Exception Handling. Run-time Errors. Methods Failure. Sometimes when the computer tries to execute a statement something goes wrong:

Internal Classes and Exceptions

Our First Programs. Programs. Hello World 10/7/2013

Exceptions. Text: Chapter19, Big C++

Exceptions. Examples of code which shows the syntax and all that

Errors and Exceptions

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

COMP200 EXCEPTIONS. OOP using Java, based on slides by Shayan Javed

Object Oriented Programming Exception Handling

Exceptions. What exceptional things might our programs run in to?

Errors. Lecture 6. Hartmut Kaiser hkaiser/fall_2011/csc1254.html

Software Design and Analysis for Engineers

GRAMMARS & PARSING. Lecture 7 CS2110 Fall 2013

11Debugging and Handling. C# Programming: From Problem Analysis to Program Design 2nd Edition. David McDonald, Ph.D. Director of Emerging Technologies

CISC-124. Passing Parameters. A Java method cannot change the value of any of the arguments passed to its parameters.

Java Errors and Exceptions. Because Murphy s Law never fails

Chapter 5 Errors. Bjarne Stroustrup

Designing Robust Classes

Defensive Programming

Object Oriented Programming. Week 7 Part 1 Exceptions

COMP1008 Exceptions. Runtime Error

CSCI 261 Computer Science II

Exception handling. Exceptions can be created by the hardware or by software: Examples. Printer out of paper End of page Divide by 0

COE318 Lecture Notes Week 10 (Nov 7, 2011)

CS11 Advanced C++ Fall Lecture 3

CS159. Nathan Sprague

Name: Checked: Preparation: Investment Calculator with input and output to text files Submit through Blackboard by 8:00am the morning of Lab.

Announcements. Lab Friday, 1-2:30 and 3-4:30 in Boot your laptop and start Forte, if you brought your laptop

Lecture 14: Exceptions 10:00 AM, Feb 26, 2018

Chapter 5 Errors. Hyunyoung Lee. Based on slides by Bjarne Stroustrup.

In this lab we will practice creating, throwing and handling exceptions.

Topic 6: Exceptions. Exceptions are a Java mechanism for dealing with errors & unusual situations

4.3 FURTHER PROGRAMMING

Exceptions Handeling

Program Correctness and Efficiency. Chapter 2

Exception handling. Aishy Amer. Lecture Outline: Introduction Types or errors or exceptions Conventional error processing Exception handling in C++

CS193j, Stanford Handout #25. Exceptions

Computer Science 9608 (Notes) Chapter: 4.3 Further programming

Lecture 14 Summary 3/9/2009. By the end of this lecture, you will be able to differentiate between errors, exceptions, and runtime exceptions.

Chapter 11: Exception Handling

Throwing exceptions 02/12/2018. Throwing objects. Exceptions

CS 251 Intermediate Programming Exceptions

Assertions and Exceptions Lecture 11 Fall 2005

CPSC 427: Object-Oriented Programming

Objectives for this class meeting. 1. Conduct review of core concepts concerning contracts and pre/post conditions

Exceptions vs. Errors Exceptions vs. RuntimeExceptions try...catch...finally throw and throws

EXCEPTIONS. Fundamentals of Computer Science I

09/08/2017 CS2530 INTERMEDIATE COMPUTING 9/8/2017 FALL 2017 MICHAEL J. HOLMES UNIVERSITY OF NORTHERN IOWA TODAY S TOPIC: Exceptions and enumerations.

Exceptions. CS162: Introduction to Computer Science II. Exceptions. Exceptions. Exceptions. Exceptions. Exceptions

CMSC 202. Exceptions

Object Orientation. A Crash Course Intro

Errors. And How to Handle Them

3 Nonlocal Exit. Quiz Program Revisited

Object Oriented Programming

Introduction to Software Design

BBM 102 Introduction to Programming II Spring Exceptions

Administration. Exceptions. Leftovers. Agenda. When Things Go Wrong. Handling Errors. CS 99 Summer 2000 Michael Clarkson Lecture 11

17. Handling Runtime Problems

Exception Handling. Chapter 9

Advanced Java Concept Unit 1. Mostly Review

Correctness and Robustness

Chapter 9. Exception Handling. Copyright 2016 Pearson Inc. All rights reserved.

PolyLarva Runtime Monitoring Framework UserManual

Laboratorio di Tecnologie dell'informazione

Kakadu and Java. David Taubman, UNSW June 3, 2003

CS504 4 th Quiz solved by MCS GROUP

CS 3 Introduction to Software Engineering. 3: Exceptions

! Errors can be dealt with at place error occurs

LECTURE 3. Compiler Phases

Exceptions. Computer Science and Engineering College of Engineering The Ohio State University. Lecture 15

To Think About. MyClass.mogrify(new int[] { 1, 2, 4, 6 }));

Comp 249 Programming Methodology Chapter 9 Exception Handling

CMSC 104 -Lecture 6 John Y. Park, adapted by C Grasso

CSE 143 Au04 Midterm 2 Sample Solution Page 1 of 7

BBM 102 Introduction to Programming II Spring 2017

Transcription:

Exceptions 08101 Programming 1 C# Programming Rob Miles

Exceptions There are two kinds of programming error Compilation error Compiler complains that our source is not valid C# Run time error Program crashes when it runs Most program crashes take the form of exceptions 25/11/2013 Rob Miles 2

Exceptional Circumstances Exceptions are created when your program does something bad: Trying to parse an invalid string Trying to position the cursor off the screen Trying to use a location outside the bounds of an array there are many others The C# language has exceptions built in for these bad things 25/11/2013 Rob Miles 3

What is an exception? An exception is a lump of data which describes something bad which has just happened You can use it to find out why your program just failed or you can ignore it Exceptions are "thrown" by a program or by the run time system If an exception is not "caught" your program will fail 25/11/2013 Rob Miles 4

Causing exceptions int i; i = int.parse("one"); This code is doomed to failure The Parse method is expecting a string that contains a number as a sequence of digits If it is given text it throws an exception 25/11/2013 Rob Miles 5

Catching exceptions try { i = int.parse("one"); } catch { Console.WriteLine("Invalid number"); } If the code in the try block causes an exception execution transfers to the catch 25/11/2013 Rob Miles 6

Safe Software By putting potentially dangerous code into a try block we can stop it from crashing our programs We could then use a loop to repeat the operation if an exception has been thrown We should put this behaviour into our number reading methods so that users cannot crash our programs 25/11/2013 Rob Miles 7

Exception objects If we wish, we can capture the exception that has been thrown We can then use this to allow our program to respond to particular errors or output extra diagnostic information The exception can be picked up by the catch clause 25/11/2013 Rob Miles 8

Catching exception details try { i = int.parse("one"); } catch (Exception e) { Console.WriteLine(e.Message); Console.WriteLine(e.StackTrace); } We can use properties in the exception object to display further information 25/11/2013 Rob Miles 9

Finally and last wishes Sometimes you want some code that will run whether the exception is thrown or not This code could close files or release resources The try catch construction can have a finally clause added to the end Statements in this clause are obeyed whether or not the exception is thrown Exceptions 25/11/2013 Rob Miles 10

The Finally Clause try { // Code that might throw an exception } catch { // Code to deal with exception } finally { // Code that is always obeyed } 25/11/2013 Rob Miles 11

Exception Types There are a variety of exceptions which can be thrown, depending on the source of the error File open exceptions, overflow exceptions, array subscript exceptions You can even create your own exception types However, if you catch the Exception type this will have the effect of catching any of these 25/11/2013 Rob Miles 12

Throwing your own exceptions You can make your program throw exceptions throw new Exception ("Bang"); This creates a new exception instance and then throws it If your program does not catch this exception it will be stopped as with any other 25/11/2013 Rob Miles 13

Exception Etiquette Exceptions should be reserved for really bad things happening You should not reject a window width value by throwing an exception They allow a program to fail in a managed way, so that you can get control when something bad happens If your program encounters something that will stop it continuing, it should throw an exception 25/11/2013 Rob Miles 14

Summary Exceptions are a way of managing errors They are an object that describes something bad that has just happened They are used when a program has good reasons why it cannot continue A program can deal with exceptions by using the try catch finally construction Exceptions 25/11/2013 Rob Miles 15