Eighth Lecture Exception Handling in Java

Slides:



Advertisements
Similar presentations
Exceptions Chapter Throwing and Catching Exceptions When a program runs into a problem that it cannot handle, it throws an exception. Exceptions.
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.
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.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 13 Exception Handling.
Exceptions and Exception Handling Carl Alphonce CSE116 March 9, 2007.
MIT-AITI Lecture 14: Exceptions Handling Errors with Exceptions Kenya 2005.
© The McGraw-Hill Companies, 2006 Chapter 15. © The McGraw-Hill Companies, 2006 Exceptions an exception is an event that occurs during the life of a program.
Exceptions Any number of exceptional circumstances may arise during program execution that cause trouble import java.io.*; class IOExample { public static.
Slides prepared by Rose Williams, Binghamton University ICS201 Exception Handling University of Hail College of Computer Science and Engineering Department.
Understand Error Handling Software Development Fundamentals LESSON 1.4.
1 Lecture 11 Interfaces and Exception Handling from Chapters 9 and 10.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 17 Exceptions and.
1 Exception Handling (in a nutshell). 2 Motivations When a program runs into a runtime error, the program terminates abnormally. How can you handle the.
Introduction to Java Chapter 11 Error Handling. Motivations When a program runs into a runtime error, the program terminates abnormally. How can you handle.
June 14, 2001Exception Handling in Java1 Richard S. Huntrods June 14, 2001 University of Calgary.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 18 Exception Handling.
Exception Handling 1. Introduction Users may use our programs in an unexpected ways. Due to design errors or coding errors, our programs may fail in unexpected.
Object Oriented Programming
Java Programming Exceptions Handling. Topics: Learn about exceptions Try code and catch Exceptions Use the Exception getMessage() method Throw and catch.
Chapter 12: Exception Handling
Exception Handling in Java Exception Handling Introduction: After completing this chapter, you will be able to comprehend the nature and kinds.
UNIT 3 TEMPLATE AND EXCEPTION HANDLING. Introduction  Program errors are also referred to as program bugs.  A C program may have one or more of four.
COMPUTER PROGRAMMING 2 Exceptions. What are Exceptions? Unexpected events that happen when the code is executing (during runtime). Exceptions are types.
Slides Credit Umair Javed LUMS Web Application Development.
Exception Handling Unit-6. Introduction An exception is a problem that arises during the execution of a program. An exception can occur for many different.
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.
Exceptions and Assertions Chapter 15 – CSCI 1302.
Exception Handling in Java Topics: Introduction Errors and Error handling Exceptions Types of Exceptions Coding Exceptions Summary.
1 Exceptions. 2 Syntax Errors, Runtime Errors, and Logic Errors syntax errors, runtime errors, and logic errors You learned that there are three categories.
Exceptions Lecture 11 COMP 401, Fall /25/2014.
Lecture10 Exception Handling Jaeki Song. Introduction Categories of errors –Compilation error The rules of language have not been followed –Runtime error.
EE4E. C++ Programming Lecture 6 Advanced Topics. Contents Introduction Introduction Exception handling in C++ Exception handling in C++  An object oriented.
Exception. Agenda Exception. Handling Exceptions. The finally Clause.
Lecture 5: Exception Handling and Text File I/O Michael Hsu CSULA.
Agenda Introduction Errors and Exception Exception Hierarchy Classification of Exceptions Built in Exceptions Exception Handling in Java User defined.
Object Throwable ErrorException RuntimeException.
Exceptions In this lecture:
Binary trees Sorting Exceptions
Tirgul 13 Exceptions 1.
Java Programming Language
Chapter 13 Exception Handling
Introduction to Exceptions in Java
Introduction Exception handling Exception Handles errors
Chapter 12 Exception Handling and Text IO
CSE 501N Fall ’09 17: Exception Handling
Exceptions 10-Nov-18.
Exceptions 10-Nov-18.
Exception Handling Chapter 9.
Exceptions Handling the unexpected
Exceptions & Error Handling
Chapter 12 Exception Handling
Exception Handling and Reading / Writing Files
Exception Handling Chapter 9 Edited by JJ.
Programming in C# CHAPTER - 7
Exception Handling Imran Rashid CTO at ManiWeber Technologies.
Chapter 13 Exception Handling
Java Exceptions Dan Fleck CS211.
Exceptions 25-Apr-19.
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.
Tutorial Exceptions Handling.
Exceptions 22-Apr-19.
Chapter 12 Exception Handling and Text IO Part 1
Exception Handling Contents
Exceptions 10-May-19.
Java Basics Exception Handling.
Exceptions 5-Jul-19.
CMSC 202 Exceptions 2nd Lecture.
Exception Handling.
Presentation transcript:

Eighth Lecture Exception Handling in Java June 14, 2001 Exception Handling in Java

Exceptions can occur at many levels: Hardware/operating system level. Arithmetic exceptions; divide by 0, under/overflow. Memory access violations; segfault, stack over/underflow. Language level. Type conversion; illegal values, improper casts. Bounds violations; illegal array indices. Bad references; null pointers. Program level. User defined exceptions.

Exception Handling in Java Topics: Introduction Errors and Error handling Exceptions Types of Exceptions Coding Exceptions Summary June 14, 2001 Exception Handling in Java

Exception Handling in Java Introduction Users have high expectations for the code we produce. Users will use our programs in unexpected ways. Due to design errors or coding errors, our programs may fail in unexpected ways during execution June 14, 2001 Exception Handling in Java

Exception Handling in Java Introduction It is our responsibility to produce quality code that does not fail unexpectedly. Consequently, we must design error handling into our programs. June 14, 2001 Exception Handling in Java

Errors and Error Handling An Error is any unexpected result obtained from a program during execution. Unhandled errors may manifest themselves as incorrect results or behavior, or as abnormal program termination. Errors should be handled by the programmer, to prevent them from reaching the user. June 14, 2001 Exception Handling in Java

Errors and Error Handling Some typical causes of errors: Memory errors (i.e. memory incorrectly allocated, memory leaks, “null pointer”) File system errors (i.e. disk is full, disk has been removed) Network errors (i.e. network is down, URL does not exist) Calculation errors (i.e. divide by 0) June 14, 2001 Exception Handling in Java

Errors and Error Handling More typical causes of errors: Array errors (i.e. accessing element –1) Conversion errors (i.e. convert ‘q’ to a number) Can you think of some others? June 14, 2001 Exception Handling in Java

Errors and Error Handling Traditional Error Handling 1. Every method returns a value (flag) indicating either success, failure, or some error condition. The calling method checks the return flag and takes appropriate action. Downside: programmer must remember to always check the return value and take appropriate action. This requires much code (methods are harder to read) and something may get overlooked. June 14, 2001 Exception Handling in Java

Errors and Error Handling Traditional Error Handling Where used: traditional programming languages (i.e. C) use this method for almost all library functions (i.e. fopen() returns a valid file or else null) June 14, 2001 Exception Handling in Java

Errors and Error Handling Traditional Error Handling 2. Create a global error handling routine, and use some form of “jump” instruction to call this routine when an error occurs. Downside: “jump” instruction (GoTo) are considered “bad programming practice” and are discouraged. Once you jump to the error routine, you cannot return to the point of origin and so must (probably) exit the program. June 14, 2001 Exception Handling in Java

Errors and Error Handling Traditional Error Handling Where used: many older programming texts (C, FORTRAN) recommended this method to programmers. Those who use this method will frequently adapt it to new languages (C++, Java). June 14, 2001 Exception Handling in Java

Errors and Error Handling Exceptions – a better error handling Exceptions are a mechanism that provides the best of both worlds. Exceptions act similar to method return flags in that any method may raise and exception should it encounter an error. Exceptions act like global error methods in that the exception mechanism is built into Java; exceptions are handled at many levels in a program, locally and/or globally. June 14, 2001 Exception Handling in Java

Exception Handling in Java Exceptions What are they? An exception is a representation of an error condition or a situation that is not the expected result of a method. Exceptions are built into the Java language and are available to all program code. Exceptions isolate the code that deals with the error condition from regular program logic. June 14, 2001 Exception Handling in Java

Exception Handling in Java Exceptions How are they used? Exceptions fall into two categories: Checked Exceptions Unchecked Exceptions Checked exceptions are inherited from the core Java class Exception. They represent exceptions that are frequently considered “non fatal” to program execution Checked exceptions must be handled in your code, or passed to parent classes for handling. June 14, 2001 Exception Handling in Java

Exception Handling in Java Exceptions How are they used? Unchecked exceptions represent error conditions that are considered “fatal” to program execution. You do not have to do anything with an unchecked exception. Your program will terminate with an appropriate error message. June 14, 2001 Exception Handling in Java

Exceptions Types of exceptions Checked exceptions – inability to acquire system resources (such as insufficient memory, file does not exist) Java checks at compile time that some mechanism is explicitly in place to receive and process an exception object that may be created during runtime due to one of these exceptions occurring. Unchecked exceptions – exceptions that occur because of the user entering bad data, or failing to enter data at all. Unchecked exceptions can be avoided by writing more robust code that protects against bad input values. Java does not check at compile time to ensure that there is a mechanism in place to handle such errors. It is often preferred to use Java’s exception handling capabilities to handle bad user input rather than trying to avoid such circumstances by providing user-input validation in the code.

Exception Handling in Java Exceptions Examples: Checked exceptions include errors such as “array index out of bounds”, “file not found” and “number format conversion”. Unchecked exceptions include errors such as “null pointer”. June 14, 2001 Exception Handling in Java

Exception Handling in Java Exceptions How do you handle exceptions? Exception handling is accomplished through the “try – catch” mechanism, or by a “throws” clause in the method declaration. For any code that throws a checked exception, you can decide to handle the exception yourself, or pass the exception “up the chain” (to a parent class). June 14, 2001 Exception Handling in Java

Exception Handling in Java Exceptions How do you handle exceptions? To handle the exception, you write a “try-catch” block. To pass the exception “up the chain”, you declare a throws clause in your method or class declaration. If the method contains code that may cause a checked exception, you MUST handle the exception OR pass the exception to the parent class (remember, every class has Object as the ultimate parent) June 14, 2001 Exception Handling in Java

Exception Handling in Java Coding Exceptions Try-Catch Mechanism Wherever your code may trigger an exception, the normal code logic is placed inside a block of code starting with the “try” keyword: After the try block, the code to handle the exception should it arise is placed in a block of code starting with the “catch” keyword. June 14, 2001 Exception Handling in Java

Exception Handling in Java Coding Exceptions Try-Catch Mechanism You may also write an optional “finally” block. This block contains code that is ALWAYS executed, either after the “try” block code, or after the “catch” block code. Finally blocks can be used for operations that must happen no matter what (i.e. cleanup operations such as closing a file) June 14, 2001 Exception Handling in Java

Exception Handling in Java Coding Exceptions Example try { … normal program code } catch(Exception e) { … exception handling code } June 14, 2001 Exception Handling in Java

Exception Handling in Java Coding Exceptions Passing the exception In any method that might throw an exception, you may declare the method as “throws” that exception, and thus avoid handling the exception yourself Example public void myMethod throws IOException { … normal code with some I/O } June 14, 2001 Exception Handling in Java

Exception Handling in Java Coding Exceptions Types of Exceptions All checked exceptions have class “Exception” as the parent class. You can use the actual exception class or the parent class when referring to an exception Where do you find the exception classes? Reference books such as “Java in a Nutshell” (O’Reilly, 2001), or the Java Documentation. June 14, 2001 Exception Handling in Java

Exception Handling in Java Coding Exceptions Types of Exceptions Examples: public void myMethod throws Exception { public void myMethod throws IOException { try { … } catch (Exception e) { … } try { … } catch (IOException ioe) { … } June 14, 2001 Exception Handling in Java

Exception Handling in Java Code Examples 1. Demonstration of an unchecked exception (NullPointerException) 2. Demonstration of checked exceptions: Passing a DivideByZeroException Handling a DivideByZeroException June 14, 2001 Exception Handling in Java

Exception Handling in Java Summary Exceptions are a powerful error handling mechanism. Exceptions in Java are built into the language. Exceptions can be handled by the programmer (try-catch), or handled by the Java environment (throws). June 14, 2001 Exception Handling in Java