Designing with Java Exception Handling

Slides:



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

Yoshi
Exception Handling. Background In a perfect world, users would never enter data in the wrong form, files they choose to open would always exist, and code.
Exception Handling The purpose of exception handling is to permit the program to catch and handle errors rather than letting the error occur and suffer.
Exceptions Don’t Frustrate Your User – Handle Errors KR – CS 1401 Spring 2005 Picture – sysprog.net.
Exception Handling Chapter 15 2 What You Will Learn Use try, throw, catch to watch for indicate exceptions handle How to process exceptions and failures.
Errors and Exceptions The objectives of this chapter are: To understand the exception handling mechanism defined in Java To explain the difference between.
 2006 Pearson Education, Inc. All rights reserved. Exception Handling in C++ CS-2303, C-Term Exception Handling in C++ CS-2303 System Programming.
 Both System.out and System.err are streams—a sequence of bytes.  System.out (the standard output stream) displays output  System.err (the standard.
Exception Handling Yaodong Bi Exception Handling Java exception handling Try blocks Throwing and re-throwing an exception Catching an.
 2002 Prentice Hall, Inc. All rights reserved. Chapter 14 – Exception Handling Outline 14.1 Introduction 14.2 When Exception Handling Should Be Used 14.3.
C++ Exception Handling
1 Lecture 11 Interfaces and Exception Handling from Chapters 9 and 10.
Exception Handling.  What are errors?  What does exception handling allow us to do?  Where are exceptions handled?  What does exception handling facilitate?
Exception Handling. Background The fact that software modules should be robust enough to work under every situation. The exception handling mechanism.
EXCEPTIONS Def: An exception is a run-time error. Examples include: attempting to divide by zero, or manipulate invalid data.
Java Exceptions, Cloning, Serialization Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Exceptions in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Handling Errors with Exception (in Java) Project 10 CSC 420.
Exception Error handling. Exception 4 n An unusual occurrence during program execution that requires immediate handling n Errors are the most common type.
Appendix A.2: Review of Java and Object-Oriented Programming: Part 2 “For the object-oriented project, remember that the primary unit of decomposition.
Java Programming Exceptions Handling. Topics: Learn about exceptions Try code and catch Exceptions Use the Exception getMessage() method Throw and catch.
COMPUTER PROGRAMMING 2 Exceptions. What are Exceptions? Unexpected events that happen when the code is executing (during runtime). Exceptions are types.
Java Software Solutions Lewis and Loftus Chapter 14 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Advanced Flow of Control --
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 12 Handling Exceptions and Events. Chapter Objectives Learn what an exception is Become aware of the hierarchy of exception classes Learn about.
© Mohamed Nuzrath Java Programming :: Chapter 6 :: Prepared & Presented By :: Mohamed Nuzrath [ Major In Programming ] NCC Programme coordinator IT Lecturer.
(13-1) Exception Handling in C++ D & D Chapter 17 Instructor - Andrew S. O’Fallon CptS 122 Washington State University.
Exceptions in C++. Exceptions  Exceptions provide a way to handle the errors generated by our programs by transferring control to functions called handlers.
Exception Handling in C++. Outline What exceptions are and when to use them Using try, catch and throw to detect, handle and indicate exceptions, respectively.
CS212: Object Oriented Analysis and Design Lecture 19: Exception Handling.
1 Exceptions. 2 Syntax Errors, Runtime Errors, and Logic Errors syntax errors, runtime errors, and logic errors You learned that there are three categories.
Lecture10 Exception Handling Jaeki Song. Introduction Categories of errors –Compilation error The rules of language have not been followed –Runtime error.
ECE122 L23: Exceptions December 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 24 Exceptions.
Object Throwable ErrorException RuntimeException.
Appendix H Exception Handling: A Deeper Look
Exception Handling in C++
16 Exception Handling.
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.
EXCEPTION HANDLING IN C++
Chapter 14 – Exception Handling
Chapter 4 Assignment Statement
MIT AITI 2003 Lecture14 Exceptions
Java Programming Language
Why exception handling in C++?
Chapter 3 Assignment Statement
Packages, Interfaces & Exception Handling
Object Oriented Programming COP3330 / CGS5409
Exception Handling and
EE422C Software Implementation II
Designing with Java Exception Handling
Chapter 11—Exceptions Handling Exceptions Throwing Exceptions.
ATS Application Programming: Java Programming
Java Programming Language
Chapter 12 Exception Handling and Text IO
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 and Reading / Writing Files
Exception Handling In Text: Chapter 14.
Exception Handling Oo28.
Exception Handling.
Exception Handling Imran Rashid CTO at ManiWeber Technologies.
Fundaments of Game Design
Exceptions (part 2) December 3, 2007 ComS 207: Programming I (in Java)
Exception Handling and Event Handling
CMSC 202 Exceptions.
Exception Handling.
ENERGY 211 / CME 211 Lecture 24 November 14, 2008.
System Models Bina Ramamurthy 9/7/2019 B.Ramamurthy.
Presentation transcript:

Designing with Java Exception Handling B.Ramamurthy 4/7/2019 B.Ramamurthy

Exception Handling When a condition arises that the currently executing code cannot handle an exception occurs. Such conditions require special exception handling facilities. Traditionally these were handled by function “return” value. (This does not satisfy any of the requirements stated next.) 4/7/2019 B.Ramamurthy

Requirements Separation between between normal (regular) code and exception handlers. Clear representation Synchronization : source and target, exception propagation Enforcement Java offers a superior Exception handling model which is very simple to use and easy to implement in any large application. Beside the above features its exception model scales very well and offers a uniform interface (for ease of use). 4/7/2019 B.Ramamurthy

Basics of Java Exception Handling Use try, throw, catch primitives. Enclose the code that you expect to result in an exception within a try block: Attach a catch block that contains the code to handle the exception, following the try block. Use throw to transfer control to catch an exception (thrown). There can be many catch blocks following a single try to process the various types of exceptions. 4/7/2019 B.Ramamurthy

Try blocks Syntax: try { normal statements; statements that may result in exceptions; //dynamic scope “throw” happens from here } 4/7/2019 B.Ramamurthy

Throwing an Exception throw is a keyword that can be used to announce that an exception has occurred. Throw normally specifies one operand. Thrown operand is an Object : an object of Exception class is thrown. When an exception is thrown, control exits the try block and proceeds to the appropriate catch handler after the try block. 4/7/2019 B.Ramamurthy

Catching an Exception Exception handlers are located within catch blocks. Each catch block starts with the keyword catch followed by parentheses containing a type of the exception this catch can handle. This is followed by the code for handling the exception enclosed within parentheses. 4/7/2019 B.Ramamurthy

Exception handling : examples For distributed computing : Ex: waiting for other host to respond. When dynamically allocating memory (large chunks). In large projects to handle error processing in a uniform manner project-wide. 4/7/2019 B.Ramamurthy

Java Exception class java.lang.Exception class : public class Exception extends Throwable { public Exception() { super();} public Exception(String s){super(s);} } 4/7/2019 B.Ramamurthy

User-defined Exception class For every project you implement you need to have a project dependent exception class so that objects of this type can be thrown. Java API also supports an extensive list of Exceptions. 4/7/2019 B.Ramamurthy

Problem Solving Build a Automatic Teller machine with simple Deposit, Withdraw, and Balance features and withdraw and deposit operation. CRC Card Design Class Design : class header, variables, methods User defined “InsufficientFundsException” and system’s “NumberFormatException” are handled, and use try, catch and throw for exception prone code. 4/7/2019 B.Ramamurthy

Summary We discussed and implement a system with robust Exception handling facility. The system we designed shows how easy it is to model even such abstract concepts as exceptions into classes and objects. 4/7/2019 B.Ramamurthy