Exception Handling CSCI293 - C# October 3, 2005.

Slides:



Advertisements
Similar presentations
Recitation 4. 2-D arrays. Exceptions. Animal[] v= new Animal[3]; 2 declaration of array v v null Create array of 3 elements a6 Animal[] null Assign.
Advertisements

Chapter 16 Exception Handling. What is Exception Handling? A method of handling errors that informs the user of the problem and prevents the program from.
Exception Handling – illustrated by Java mMIC-SFT November 2003 Anders P. Ravn Aalborg University.
Exceptions & exception handling Use sparingly. Things you can do with exceptions: 1. Define a new exception class. 2. Create an exception instance. 3.
Lecture 9. 2 Exception  An exception is a unusual, often unpredictable event, detectable by software or hardware, that requires special processing occurring.
Exceptions Don’t Frustrate Your User – Handle Errors KR – CS 1401 Spring 2005 Picture – sysprog.net.
Designing an ADT The design of an ADT should evolve naturally during the problem-solving process Questions to ask when designing an ADT What data does.
Mahmoud Rafeek Alfarra Computer Programming || Chapter 2: Exception handling.
SE-1020 Dr. Mark L. Hornick 1 Exceptions and Exception Handling.
Exceptions Any number of exceptional circumstances may arise during program execution that cause trouble import java.io.*; class IOExample { public static.
Lesson 16 Exceptions Lesson Exceptions1. Murphy’s Law Anything that can go wrong will go wrong Lesson Exceptions2.
Lecture 27 Exceptions COMP1681 / SE15 Introduction to Programming.
Lecture 28 More on Exceptions COMP1681 / SE15 Introduction to Programming.
1 Lecture#8: EXCEPTION HANDLING Overview l What exceptions should be handled or thrown ? l The syntax of the try statement. l The semantics of the try.
CS1101: Programming Methodology Aaron Tan.
06 Exception Handling. 2 Contents What is an Exception? Exception-handling in Java Types of Exceptions Exception Hierarchy try-catch()-finally Statement.
Java Programming Exception Handling. The exception handling is one of the powerful mechanism provided in java. It provides the mechanism to handle the.
1.  Collections are data structures that holds data in different ways for flexible operations  C# Collection classes are defined as part of the ◦ System.Collections.
CMSC 202 Exceptions. Aug 7, Error Handling In the ideal world, all errors would occur when your code is compiled. That won’t happen. Errors which.
Basic Java Programming CSCI 392 Week Two. Stuff that is the same as C++ for loops and while loops for (int i=0; i
Handling Exceptions in java. Exception handling blocks try { body-code } catch (exception-classname variable-name) { handler-code }
Exception Handling in JAVA. Introduction Exception is an abnormal condition that arises when executing a program. In the languages that do not support.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 10.
ILM Proprietary and Confidential -
Sadegh Aliakbary Sharif University of Technology Fall 2010.
JAVA COURSE LESSON2 BY OMPUTER ENGINEEING ASSOCIATION.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
1 cuberoot /* Solve f(x) = x*x*x-5 = 0 f'(x) = 3x^2 */ public class cuberoot{ public static void main(String args[]){ double error= ; double x0.
1 Advanced Programming Examples Output. Show the exact output produced by the following code segment. char[,] pic = new char[6,6]; for (int i = 0; i
Exception Handling SWE 344 Internet Protocols & Client Server Programming.
Exceptions an unusual condition – e.g. division by zero – e.g. file doesn't exist – e.g. illegal type – etc. etc… typically a run-time error – i.e. during.
Exceptions and Error Handling. Exceptions Errors that occur during program execution We should try to ‘gracefully’ deal with the error Not like this.
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.
Lecture 11 Dr. Eng. Ibrahim El-Nahry Exception Handling.
CSE 1201 Object Oriented Programming
Exceptions handling Try, catch blocks Throwing exceptions.
Exceptions Exceptions are used to signal that an unexpected event has happened in a program C++ will generate exceptions for some errors in the program.
Topics: Templates Exceptions
Exceptions In this lecture:
Exceptions: When things go wrong
Chapter 10 – Exception Handling
Testing and Exceptions
Exceptions C++ Interlude 3
Advanced Programming Behnam Hatami Fall 2017.
E x c e p t i o n s Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live. — Martin Golding.
CNS 3260 C# .NET Software Development
Functions Used to write code only once Can use parameters.
Exceptions & exception handling
ATS Application Programming: Java Programming
Exceptions & exception handling
Chapter 17 Templates and Exceptions Part 2
COMPUTER 2430 Object Oriented Programming and Data Structures I
Exceptions Problems in a Java program may cause exceptions or errors representing unusual or invalid processing. An exception is an object that defines.
Exercise 11.1 Write a code fragment that performs the same function as the statement below without using the crash method Toolbox.crash(amount < 0,
Fundamental Error Handling
Java Exception Very slightly modified from K.P. Chow
Exception Handling in Java
Java Exception Very slightly modified from K.P. Chow
CS 180 Assignment 6 Arrays.
class PrintOnetoTen { public static void main(String args[]) {
Chapter 13 Exception Handling: A Deeper Look
Exceptions handling Try, catch blocks Throwing exceptions.
Module 4 Loops and Repetition 4/15/2019 CSE 1321 Module 4.
Errors and Exceptions Error Errors are the wrongs that can make a program to go wrong. An error may produce an incorrect output or may terminate the execution.
E x c e p t i o n s Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live. — Martin Golding.
Why do we need exceptions?
Lecture 9.
Java Basics Exception Handling.
CMSC 202 Exceptions.
Basic Exception Handling
Presentation transcript:

Exception Handling CSCI293 - C# October 3, 2005

Definition An exception is an event that occurs during the execution of a program that disrupts the normal flow of instructions during the execution of a program.

How Exceptions are Processed Suppose that main calls function A which calls function B: If a statement in B causes an exception and that statement is not inside a try block, then function B throws the exception up to function A. If A does not handle the exception, then the exception is thrown up to main. If main throws an exception, the program crashes.

static void Main(string[] args) { Console.WriteLine("inside main"); func1(); Console.WriteLine("leaving main"); } static void func1() Console.WriteLine("inside func1"); func2(); Console.WriteLine("leaving func1"); static void func2() Console.WriteLine("inside func2"); int a = 0, b=9, c; c = b/a; Console.WriteLine("leaving func2");

Try Statement try catch { code that may have a problem } "Do, or do not. There is no 'try'". -- Yoda try { code that may have a problem } catch what to do if error occurs

static void Main(string[] args) { Console.WriteLine("inside main"); func1(); Console.WriteLine("leaving main"); } static void func1() Console.WriteLine("inside func1"); try func2(); catch Console.WriteLine("func1 caught an execption"); Console.WriteLine("leaving func1"); static void func2() Console.WriteLine("inside func2"); int a = 0, b=9, c; c = b/a; Console.WriteLine("leaving func2");

If at first you don’t succeed, try, try again... { func2(); } catch (System.ArithmeticException e) Console.WriteLine("func1 - " + e.Message); catch Console.WriteLine("func1 - unknown exception

Create your own... Exceptions are objects, like everything else: public class MyException : System.ApplicationException { public MyException (string msg) base (msg) }

Intentionally Causing Problems Assuming errorHasOccurred is one of our variables... if (errorHasOccurred) throw new MyException();

int[] list = new int[10]; string inputline; int intvalue = 0; Console.WriteLine("Enter ten integers"); for (int i=0; i<10; i++) { inputline = Console.ReadLine(); try intvalue = Convert.ToInt32(inputline); } catch { } list[i] = intvalue; Console.WriteLine("bob contains: "); foreach (int x in list) Console.Write("{0} ", x); Console.WriteLine();

int[] list = new int[10]; string inputline; int intvalue = 0; Console.WriteLine("Enter ten integers"); for (int i=0; i<10; i++) try { inputline = Console.ReadLine(); intvalue = Convert.ToInt32(inputline); list[i] = intvalue; } catch { } Console.WriteLine("bob contains: "); foreach (int x in list) Console.Write("{0} ", x); Console.WriteLine();