Basic Exception Handling

Slides:



Advertisements
Similar presentations
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.
Advertisements

Exceptions & exception handling Use sparingly. Things you can do with exceptions: 1. Define a new exception class. 2. Create an exception instance. 3.
Slides prepared by Rose Williams, Binghamton University Chapter 9 Exception Handling.
CS102--Object Oriented Programming
Comp 249 Programming Methodology
1 Week 11 l Basic Exception Handling »the mechanics of exceptions l Defining and Using Exceptions »some "simple" cases l Reality Check »guidelines for.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 16 Exception Handling.
Exception Handling Chapter 8. Outline Basic Exception Handling Defining Exception Classes Using Exception Classes.
Java I/O Java I/O is based on input streams and output streams. All input and output are defined in the Java IO package. 1.
Exception Handling By: Thomas Fasciano. What is an Exception?  An error condition that occurs during the execution of a Java Program.
Chapter 8Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 8 l Basic Exception Handling »the mechanics of exceptions l.
Exceptions Any number of exceptional circumstances may arise during program execution that cause trouble import java.io.*; class IOExample { public static.
Exception Handling Chapter 12.  Errors- the various bugs, blunders, typos and other problems that stop a program from running successfully  Natural.
Slides prepared by Rose Williams, Binghamton University ICS201 Exception Handling University of Hail College of Computer Science and Engineering Department.
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. Chapter 16 Exception Handling.
1 Lecture 11 Interfaces and Exception Handling from Chapters 9 and 10.
Lecture 27 Exceptions COMP1681 / SE15 Introduction to Programming.
Slides prepared by Rose Williams, Binghamton University Chapter 9 Exception Handling.
CS102--Object Oriented Programming Lecture 11: Exception Handling Copyright © 2008 Xiaoyan Li.
CSE 11 February 6, © 2003 Walter Savitch These slides are for the exclusive use of students in CSE 11 at UCSD, Winter quarter They may not.
Chapter 81 Exception Handling Chapter 8. 2 Reminders Project 5 due Oct 10:30 pm Project 3 regrades due by midnight tonight Discussion groups now.
Slides prepared by Rose Williams, Binghamton University Chapter 9 More Exception Handling.
Exception Handling Recitation – 10/(23,24)/2008 CS 180 Department of Computer Science, Purdue University.
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.
Exception Handling (Chapter 8) CS 180 Recitation - February 29, 2008 Department of Computer Science Purdue University.
Java Exception Handling
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.
Introduction to Computer Programming Error Handling.
Chapter 81 Exception Handling Chapter 8. 2 Objectives become familiar with the notion of exception handling learn Java syntax for exception handling learn.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 9 : Exception Handling King Fahd University of Petroleum & Minerals College of Computer.
220 FINAL TEST REVIEW SESSION Omar Abdelwahab. INHERITANCE AND POLYMORPHISM Suppose you have a class FunClass with public methods show, tell, and smile.
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.
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.
Java I/O Java I/O is based on input streams and output streams. All input and output are defined in the Java IO package. 1.
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.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
Computer Programming with JAVA Chapter 8. Exception Handling Basic Exception Handling the mechanics of exceptions Defining and Using Exceptions some "simple"
Sheet 3 HANDLING EXCEPTIONS Advanced Programming using Java By Nora Alaqeel.
Programming and Problem Solving With Java Copyright 1999, James M. Slack Exceptions Handling Exceptions with try and catch The finally-block The throws.
Class and Method Definitions. UML Class Diagram Automobile - fuel: double - speed: double - license: String + increaseSpeed(double howHardPress): void.
Lecturer: Dr. AJ Bieszczad Chapter 8 COMP 150: Introduction to Object-Oriented Programming 8-1 l Basic Exception Handling »the mechanics of exceptions.
Throw, Throws & Try-Catch Statements Explanations and Pictures from: Reference:
Exceptions and Error Handling. Exceptions Errors that occur during program execution We should try to ‘gracefully’ deal with the error Not like this.
Today… StringTokenizer class. Method Overloading. Catching Exceptions (and what they are!). Start Pointers and Aliasing. Winter 2016CMPE212 - Prof. McLeod1.
Chapter 16 Exception Handling
Exceptions: When things go wrong
Chapter 10 – Exception Handling
MIT AITI 2003 Lecture14 Exceptions
Chapter 9 Exception Handling
Testing and Exceptions
Exceptions C++ Interlude 3
Something about Java Introduction to Problem Solving and Programming 1.
Exceptions 10-Nov-18.
Announcements/Reminders
Handling Exceptions.
Exception Handling Chapter 9.
Exception Handling Chapter 8 Basic Exception Handling
Exceptions & exception handling
CMSC 202 Exceptions.
Exceptions & exception handling
Exception Handling CSCI293 - C# October 3, 2005.
Exception Handling Chapter 9 Edited by JJ.
The Basics of Recursion
Introduction to Java Brief history of Java Sample Java Program
Chapter 9 Exception Handling
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.
Exception Handling Chapter 8 Basic Exception Handling
CMSC 202 Exceptions.
Java Programming: From Problem Analysis to Program Design, 4e
Presentation transcript:

Basic Exception Handling

Exceptions in Java Either the Java language itself or your code provides a mechanism that signals when something unusual happens. This is called throwing an exception. Another part of your code contains code for handling the exception.

Example of Exception Handling import java.util.*; public class GotMilk { public static void main(String[] args) { int donutCount, milkCount; double donutsPerGlass; Scanner keyboard = new Scanner(System.in); System.out.println("Enter number of donuts:"); donutCount = keyboard.nextInt( ); System.out.println("Enter number of glasses of milk:"); milkCount = keyboard.nextInt( ); if (milkCount < 1) { System.out.println("No Milk!"); System.out.println("Go buy some milk."); } else { donutsPerGlass = donutCount/(double)milkCount; System.out.println(donutCount + " donuts."); System.out.println(milkCount + " glasses of milk."); System.out.println("You have " + donutsPerGlass + " donuts for each glass of milk."); } System.out.println("End of program."); } }

Example of Exception Handling (cont’d) import java.util.*; public class ExceptionDemo { public static void main(String[] args) { int donutCount, milkCount; double donutsPerGlass; Scanner keyboard = new Scanner(System.in); try { System.out.println("Enter number of donuts:"); donutCount = keyboard.nextInt( ); System.out.println("Enter number of glasses of milk:"); milkCount = keyboard.nextInt( ); if (milkCount < 1) throw new Exception("Exception: No Milk!"); donutsPerGlass = donutCount/(double)milkCount; System.out.println(donutCount + " donuts."); System.out.println(milkCount + " glasses of milk."); System.out.println("You have " + donutsPerGlass + " donuts for each glass of milk."); } catch(Exception e) { System.out.println(e.getMessage( )); System.out.println("Go buy some milk."); } System.out.println("End of program."); } }

How Exception Handling Works Exception is a predefined class. The throw statement creates an object of the class Exception and throws it. When an exception is thrown, the code in the surrounding try block stops execution, and another portion of code, know as a catch block, begins execution. Execution of the catch block is called catching the exception.

The try Block The basic way of handling exceptions in Java consists of the try-throw-catch threesome. A try block contains the code for the basic algorithm that tells the computer what to do when everything goes smoothly. It is called try because you are not 100 percent sure everything will go smoothly.

The throw Statement If something goes wrong, you want to throw an exception. The throw statement creates a new object of the class Exception and throws it. When an exception is thrown, the code in the try block stops execution, and another portion of code, the catch block, begins execution.

The throw Statement (cont’d) In the example, new Exception(“Exception: No Milk!”) the string “Exception: No Milk!” is an argument for the constructor for the class Exception. The Exception object, created with new, stores this string in an instance variable of the object, so that it can be recovered in the catch block.

The catch block Although it is not a method definition it looks a little like one. It is a separate piece of code that is executed when a program throws an exception. The throw statement is similar to a method call, but instead of calling a method, it calls the catch block.

The catch Block Parameter The following the word catch is the catch-block parameter. The class name preceding the catch-block parameter specifies what kind of exception the catch-block can catch. The catch-block parameter gives you a name for the exception that is caught, so that you can write code in the catch block in order to manipulate that exception object.

The getMessage Method Every exception has a String instance variable that contains some message, which typically identifies the reason for the exception. If the object is called e, then the method call e.getMessage() returns this string.