Exception Handling and Reading / Writing Files

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

Lecture 23 Input and output with files –(Sections 2.13, 8.7, 8.8) Exceptions and exception handling –(Chapter 17)
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.
EXCEPTIONS. What’s an exception?? Change the flow of control when something important happens ideally - we catch errors at compile time doesn’t happen.
Exceptions and Exception Handling Carl Alphonce CSE116.
Slides prepared by Rose Williams, Binghamton University ICS201 Exception Handling University of Hail College of Computer Science and Engineering Department.
Exceptions and Exception Handling (1) Carl Alphonce CSE116.
Exceptions and Exception Handling (2) Carl Alphonce CSE116.
EXCEPTIONS Def: An exception is a run-time error. Examples include: attempting to divide by zero, or manipulate invalid data.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 17 Exceptions and.
Exceptions Three categories of errors: Syntax errors Runtime errors Logic errors Syntax errors: rules of the language have not been followed. Runtime error:
Exceptions Used to signal errors or unexpected situations to calling code Should not be used for problems that can be dealt with reasonably within local.
Exceptions in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
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.
Java Software Solutions Foundations of Program Design Sixth Edition
Exception Handling. Exceptions and Errors When a problem encounters and unexpected termination or fault, it is called an exception When we try and divide.
June 14, 2001Exception Handling in Java1 Richard S. Huntrods June 14, 2001 University of Calgary.
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.
Chapter 12: Exception Handling
Slides Credit Umair Javed LUMS Web Application Development.
Exceptions Handling the unexpected. RHS – SWC 2 The Real World So far, most of our code has been somewhat näive We have assumed that nothing goes wrong…
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.
BIO Java 1 Exception Handling Aborting program not always a good idea – can’t lose messages – E-commerce: must ensure correct handling of private.
Sheet 3 HANDLING EXCEPTIONS Advanced Programming using Java By Nora Alaqeel.
Exception Handling in Java Topics: Introduction Errors and Error handling Exceptions Types of Exceptions Coding Exceptions Summary.
Exceptions in Java. What is an exception? An exception is an error condition that changes the normal flow of control in a program Exceptions in Java separates.
Exceptions Handling Prepared by: Ligemm Mae del Castillo.
1 Exceptions. 2 Syntax Errors, Runtime Errors, and Logic Errors syntax errors, runtime errors, and logic errors You learned that there are three categories.
COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi.
Lecture10 Exception Handling Jaeki Song. Introduction Categories of errors –Compilation error The rules of language have not been followed –Runtime error.
And other languages…. must remember to check return value OR, must pass label/exception handler to every function Caller Function return status Caller.
Exception. Agenda Exception. Handling Exceptions. The finally Clause.
ECE122 L23: Exceptions December 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 24 Exceptions.
Exceptions and Error Handling. Exceptions Errors that occur during program execution We should try to ‘gracefully’ deal with the error Not like this.
Lecture 5: Exception Handling and Text File I/O Michael Hsu CSULA.
Object Throwable ErrorException RuntimeException.
Eighth Lecture Exception Handling in Java
Java Exceptions a quick review….
Tirgul 13 Exceptions 1.
MIT AITI 2003 Lecture14 Exceptions
Chapter 13 Exception Handling
Introduction to Exceptions in Java
Introduction Exception handling Exception Handles errors
Introduction to Exceptions in Java
Testing and Exceptions
CSE 501N Fall ’09 17: Exception Handling
Chapter 14: Exception Handling
E x c e p t i o n s Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live. — Martin Golding.
Advanced Java Programming
ATS Application Programming: Java Programming
Exceptions Handling the unexpected
Chapter 12 Exception Handling
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 Chapter 9 Edited by JJ.
Exception Handling.
CSE 143 Java Exceptions 1/18/2019.
Chapter 13 Exception Handling
Lecture 11 Objectives Learn what an exception is.
Java Exceptions Dan Fleck CS211.
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.
Java Programming Exceptions CSC 444 By Ralph B. Bisland, Jr.
Chapter 12 Exception Handling and Text IO Part 1
Java Basics Exception Handling.
Java Programming: From Problem Analysis to Program Design, 4e
Presentation transcript:

Exception Handling and Reading / Writing Files

Topics What is an Exception? Basic error classes in Java Checked Exceptions vs. Unchecked Exceptions Exception handling using throws Reading from files and writing to files Exception Handling using try/catch

What is an Exception? An exception is an unexpected error condition that prevents the program from continuing normally. Programs must be flexible when handling problems. Rarely do we want to terminate the program abruptly. For example, if the program detects an attempt to read from a non-existent file, we might want to ask the user for another file name. Exception handling provides an adaptable mechanism for handling such problems.

Examples of Exceptions Reading from a file that does not exist. Reading from a file that has corrupt data. Writing to a file, but the disk is full. Reading input from the user and the user enters an invalid data type. Unexpected division by zero Accessing an array with a subscript (index) that is out of bounds.

Basic Error Classes Java includes two basic classes of errors: Exception and Error.   Exception IOException RuntimeException ArithmeticException IndexOutOfBoundsException ArrayIndexOutOfBoundException NoSuchElementException InputMismatchException etc. Error: Represents serious errors which your program usually cannot recover. VirtualMachineError OutOfMemoryError : insufficient memory InternalError

Checked vs. Unchecked Exceptions There are two categories of Exceptions: Checked and Unchecked.   Unchecked: Means not verified during compile time. These errors typically occur because of poor programming. For example, an array index out of bound. Checked: Means checked at compile time, such as FileNotFoundException. Code that uses a checked exception will not compile if the catch is not handled. The following code has an unhandled exception

Example of an Exception using an Array. In the code below, an exception occurs because the array index is out of bounds. When this happens, an object of the exception class is thrown. int [] arr = {11, 9, 6, 5, 8}; i = 10; arr[i] *= 2;  

Method 1: Basic Exception Handling using throw. When an error condition is detected, we can throw an appropriate exception object to handle the condition. if (testScore > 100) { throw new IllegalArgumentException (“Invalid Test Score”); } gpa = (testScore + hw + quiz) / 3;   The last line is not executed when the exception is thrown.

Example 1: Exception Handling using throw. An error condition is detected An IllegalArgumentException is throw to handle the error. Is this a practical example of exception handling?

Method 2: Writing to a file Exception Handling using a throws IOException .

Method 3: Exception Handling using try/catch Blocks Detect and handle exceptions with the try/catch statements. Place the statements that can cause an exception inside a try block, and the handler inside a catch clause. The try block contains one or more statements that may cause an exception. Each catch clause contains the handler for an exception.

Example 1: Writing to a file Exception Handling using a try/catch.

Example 2: Reading from a file Exception Handling using a try/catch Add a finally element to complete any remaining tasks.

Creating an Exception Class An Exception class can be created by inheriting from java.lang.Exception. public class MyException extends Exception{ //Required inherited methods are coded here. } thow and catch can be used with this class.

Example : Create an exception class and test it.