Chapter 3 Java Exception

Similar documents
Java Exception. Wang Yang

Lecture 20. Java Exceptional Event Handling. Dr. Martin O Connor CA166

Object oriented programming. Instructor: Masoud Asghari Web page: Ch: 7

Assoc. Prof. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

ECE 122. Engineering Problem Solving with Java

Data Structures. 02 Exception Handling

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

BBM 102 Introduction to Programming II Spring Exceptions

EXCEPTIONS. Objectives. The try and catch Statements. Define exceptions. Use try, catch and finally statements. Describe exception categories

EXCEPTIONS. Java Programming

Chapter 13 Exception Handling

BBM 102 Introduction to Programming II Spring 2017

Exceptions in Java

Exception Handling. Chapter 9

Exceptions. References. Exceptions. Exceptional Conditions. CSE 413, Autumn 2005 Programming Languages

Exceptions. CSE 142, Summer 2002 Computer Programming 1.

Exceptions. Readings and References. Exceptions. Exceptional Conditions. Reading. CSE 142, Summer 2002 Computer Programming 1.

EXCEPTION HANDLING. Summer 2018

Sri Vidya College of Engineering & Technology Question Bank

ASSERTIONS AND LOGGING

More on Exception Handling

More on Exception Handling

A problem?... Exceptions. A problem?... A problem?... Suggested Reading: Bruce Eckel, Thinking in Java (Fourth Edition) Error Handling with Exceptions

CSC207H: Software Design. Exceptions. CSC207 Winter 2018

Correctness and Robustness

Programming II (CS300)

Java Errors and Exceptions. Because Murphy s Law never fails

Exceptions. Produced by. Introduction to the Java Programming Language. Eamonn de Leastar

Exceptions. CSC207 Winter 2017

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

What is the purpose of exceptions and exception handling? Vocabulary: throw/raise and catch/handle Exception propagation Java checked and unchecked

17. Handling Runtime Problems

Exceptions and I/O: sections Introductory Programming. Errors in programs. Exceptions

Introductory Programming Exceptions and I/O: sections

Internal Classes and Exceptions

Programming II (CS300)

Exceptions. Produced by. Algorithms. Eamonn de Leastar Department of Computing, Maths & Physics Waterford Institute of Technology

CS 3 Introduction to Software Engineering. 3: Exceptions

Fundamentals of Object Oriented Programming

Object Oriented Programming

6.Introducing Classes 9. Exceptions

PIC 20A Exceptions. Ernest Ryu UCLA Mathematics. Last edited: November 27, 2017

A Third Look At Java. Chapter Seventeen Modern Programming Languages, 2nd ed. 1

Writing your own Exceptions. How to extend Exception

CS 61B Data Structures and Programming Methodology. July 7, 2008 David Sun

16-Dec-10. Consider the following method:

13: Error Handling with Exceptions. The basic philosophy of Java is that "badly formed code will not be run."


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

Chapter 12 Exception Handling and Text IO. Liang, Introduction to Java Programming, Tenth Edition, Global Edition. Pearson Education Limited

CS193j, Stanford Handout #25. Exceptions

Exceptions, try - catch - finally, throws keyword. JAVA Standard Edition

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

15CS45 : OBJECT ORIENTED CONCEPTS

Program Correctness and Efficiency. Chapter 2

National University. Faculty of Computer Since and Technology Object Oriented Programming

Motivations. Chapter 12 Exceptions and File Input/Output

Introduction to Computation and Problem Solving. Class 25: Error Handling in Java. Prof. Steven R. Lerman and Dr. V. Judson Harward.

Chapter 12 Exception Handling

Exception Handling. Exception Handling

CS115. Chapter 17 Exception Handling. Prof. Joe X. Zhou Department of Computer Science. To know what is exception and what is exception handling

CSCI 261 Computer Science II

C16b: Exception Handling

When we reach the line "z = x / y" the program crashes with the message:

EXCEPTION-HANDLING INTRIVIEW QUESTIONS

Exceptions - Example. Exceptions - Example

Input, Output and Exceptions. COMS W1007 Introduction to Computer Science. Christopher Conway 24 June 2003

Object Oriented Programming

Lecture 19 Programming Exceptions CSE11 Fall 2013

Abstract Classes, Exceptions

CS Programming I: Exceptions

ITI Introduction to Computing II

Input-Output and Exception Handling

Projeto de Software / Programação 3 Tratamento de Exceções. Baldoino Fonseca/Márcio Ribeiro

CS 112 Programming 2. Lecture 08. Exception Handling & Text I/O (1) Chapter 12 Exception Handling and Text IO

Chapter 8. Exception Handling. CS 180 Sunil Prabhakar Department of Computer Science Purdue University

Introduction to Software Design

Course Status Polymorphism Containers Exceptions Midterm Review. CS Java. Introduction to Java. Andy Mroczkowski

Exceptions & Miscellaneous Lecture 17

Exception-Handling Overview

Object Oriented Programming. Week 7 Part 1 Exceptions

I/O Streams. program. Standard I/O. File I/O: Setting up streams from files. program. File I/O and Exceptions. Dr. Papalaskari 1

I/O Streams. program. Standard I/O. File I/O: Setting up streams from files. program. File I/O and Exceptions. Dr. Papalaskari 1

ITI Introduction to Computing II

Exception Handling. Chapter 11. Outline. Example: The Quotient app What Are Exceptions? Java By Abstraction Chapter 11

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

Introduction Unit 4: Input, output and exceptions

CSE 331 Software Design & Implementation

Exception Handling CSCI 201 Principles of Software Development

Exception Handling Introduction. Error-Prevention Tip 13.1 OBJECTIVES

Practical Session #09 Exceptions & Networking. Tom Mahler

Comp 249 Programming Methodology Chapter 9 Exception Handling

Exception Handling in Java

Defensive Programming. Ric Glassey

Exceptions and Error Handling

COSC 123 Computer Creativity. I/O Streams and Exceptions. Dr. Ramon Lawrence University of British Columbia Okanagan

Exceptions and assertions

Review. Errors. Ø Users may enter data in the wrong form Ø Files may not exist Ø Program code has bugs!*

Java Programming Training for Experienced Programmers (5 Days)

Video 2.1. Arvind Bhusnurmath. Property of Penn Engineering, Arvind Bhusnurmath. SD1x-2 1

Transcription:

Chapter 3 Java Exception J AVA C O S E @ Q Q. C O M

Content A Notion of Exception Java Exceptions Exception Handling User-defined Exceptions How to Use Exception 2 COSE Java

Exceptional Condition Divided by 0 No input or output file Visit a Null reference Exceptional Condition A Sound Program Should Declare the possible exceptional condition Handle the exceptions at right time and in right place 3

Exceptional Condition Exceptional Condition VS. Normal Condition End of file is a normal condition Normal condition does not lead to program halt Exceptional conditions lead to program halt Exceptional Condition VS. Error Condition JVM crash is an error condition Error conditions cannot be handled by program Exceptional conditions can and should be handled by program 4

Java Exception Significance of Exception Offering a clear grammar for handling program correctness Balancing between Clarity and Correctness 5

Java Exception 7

Runtime Exception Throwable Exceptions in Normal Run-time Related to Execution of Program Not Related to the Business Logic Difference: DO NOT HAVE TO Declare Example: NullPointerException ArithmeticException 8 COSE Java

Java Exception Handling Java Use Following Exception Handling Process: Block A meets an exceptional condition Block A halt One or more exception objects generated Exception objects are thrown JVM looks for proper codes to catch these objects Exceptional condition is handled Program continues from exceptional handling code 9

Java Exceptions 10 Exception DataFormatException ClassNotFoundException IOException SQLException TimeoutException SocketException ArrayIndexOutOfBounsExcepti on NullPointerException Superclass of all exceptions Error data format Exception in class loading IO operation error Database operation error Timeout error Socket operation error Exception in visiting array Visiting null reference

Declare Exception throw and throws Declare Possible Exceptions throw throws exceptions in method body throws defines Exception Specification 11

Catch and Handling of Exceptions try, catch and finally try is used to monitor method invocation catch is used to catch thrown exceptions finally is used for execute essential code whether there are exceptions or not Multiple catch clause At least one catch or finally clause 12

Deep Into FINALLY 14 The finally block always executes when the try block exits. This ensures that the finally block is executed even if an unexpected exception occurs. But finally is useful for more than just exception handling it allows the programmer to avoid having cleanup code accidentally bypassed by a return, continue, or break. Putting cleanup code in a finally block is always a good practice, even when no exceptions are anticipated. Note: If the JVM exits while the try or catch code is being executed, then the finally block may not execute. Likewise, if the thread executing the try or catch code is interrupted or killed, the finally block may not execute even though the application as a whole continues. No catch Return before exception occurs COSE Java

Guess the result: Deep Into FINALLY 15

Guess the result: Deep Into FINALLY 16

Deep Into FINALLY 17 http://www.ibm.com/developerworks/cn/java/jlo-finally/

Rethrow Exceptions 18 Is there something wrong?

Exception Handling 19 Message new Exception(String message) getmessage() Cause initcause() new Exception(Exception cause) getcause() StackTrace printstacktrace()

User-defined Exception 20

Think 21 Exception in Overriding Which is allowed?

Self-study 22 Assertions( 断言 ) Used for Software Testing Regular Expression( 正则表达式 ) java.util.regex

Forecast 23 Java I/O Introduction File and Directory Byte-stream and Character-stream Bridge between b-s and c-s Random Access File Standard I/O System.in System.out java.nio Pilot