Designing with Java Exception Handling

Slides:



Advertisements
Similar presentations
Pearson Education, Inc. All rights reserved. 1.. Exception Handling.
Advertisements

Topics Introduction Types of Errors Exceptions Exception Handling
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.
An Introduction to Java Programming and Object- Oriented Application Development Chapter 8 Exceptions and Assertions.
Review Linked list: Doubly linked list, insertback, insertbefore Remove Search.
 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.
Exception Handling 1 CISC6795, Spring Introduction 2 Exception – an indication of a problem that occurs during a program’s execution, for examples:
 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. Background The fact that software modules should be robust enough to work under every situation. The exception handling mechanism.
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.
06 Exception Handling. 2 Contents What is an Exception? Exception-handling in Java Types of Exceptions Exception Hierarchy try-catch()-finally Statement.
Java Programming Exceptions Handling. Topics: Learn about exceptions Try code and catch Exceptions Use the Exception getMessage() method Throw and catch.
Exception Handling in Java Exception Handling Introduction: After completing this chapter, you will be able to comprehend the nature and kinds.
Java Software Solutions Lewis and Loftus Chapter 14 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Advanced Flow of Control --
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 10.
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.
(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.
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
Java Programming Language
Introduction to Exceptions in Java
Why exception handling in C++?
Introduction to Exceptions in Java
Part IX Fundamentals of C and C++ Programming Exception Handling
Chapter 3 Assignment Statement
Object Oriented Programming COP3330 / CGS5409
Advanced Programming Behnam Hatami Fall 2017.
Exception Handling and
EE422C Software Implementation II
Chapter 11—Exceptions Handling Exceptions Throwing Exceptions.
ATS Application Programming: Java Programming
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.
Designing with Java Exception Handling
Exceptions (part 2) December 3, 2007 ComS 207: Programming I (in Java)
Exception Handling and Event Handling
CMSC 202 Exceptions.
Exception Handling.
System Models Bina Ramamurthy 9/7/2019 B.Ramamurthy.
Presentation transcript:

Designing with Java Exception Handling B.Ramamurthy 11/16/2018 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.) 11/16/2018 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. Besides the above features its exception model scales very well and offers a uniform interface (for ease of use). 11/16/2018 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. “throws” keyword is used to indicate which methods have the potential to throw an exception. When calling these methods the calls have to be enclosed within try blocks. 11/16/2018 B.Ramamurthy

Try blocks Syntax: try { normal statements; statements that may result in exceptions; //dynamic scope “throw” happens from here } 11/16/2018 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. 11/16/2018 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 curly brackets. 11/16/2018 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. Simple input validation 11/16/2018 B.Ramamurthy

Java Exception class java.lang.Exception class : public class Exception extends Throwable { public Exception() { super();} public Exception(String s){super(s);} } 11/16/2018 B.Ramamurthy

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

Problem Solving Build a Automatic Teller machine with simple Deposit, Withdraw, and Balance features and withdraw and deposit operation. User defined “InsufficientFundsException” and system’s “NumberFormatException” are handled, and use try, catch and throw for exception prone code. See code: 11/16/2018 B.Ramamurthy

Summary We discussed and implement a robust system with Exception handling facility. The system we designed shows how simple it is to model even such abstract concepts as exceptions into classes and objects and to make your systems robust. 11/16/2018 B.Ramamurthy