Introduction to Exceptions in Java CS201, SW Development Methods.

Slides:



Advertisements
Similar presentations
Exceptions CSE301 University of Sunderland Harry Erwin, PhD.
Advertisements

Exceptions: when things go wrong. Various sources of error public static doSomething() { int i = 3.0; while(!done); { int i = false } ) Syntactic errors.
Yoshi
Lecture 23 Input and output with files –(Sections 2.13, 8.7, 8.8) Exceptions and exception handling –(Chapter 17)
CS102--Object Oriented Programming
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.
Exception Handling Xiaoliang Wang, Darren Freeman, George Blank.
Exceptions1 Syntax, semantics, and pragmatics. Exceptions2 Syntax, semantics, pragmatics Syntax –How it looks, i.e. how we have to program to satisfy.
SE-1020 Dr. Mark L. Hornick 1 Exceptions and Exception Handling.
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.
Jerry Lebowitz. Topics  Provides a facility for a systematic object oriented approach to handling runtime errors ◦ Can also handle runtime errors.
Exceptions1 Syntax, semantics, and pragmatics. Exceptions2 Syntax, semantics, pragmatics Syntax –How it looks, i.e. how we have to program to satisfy.
Lecture 27 Exceptions COMP1681 / SE15 Introduction to Programming.
Exception Handling.  What are errors?  What does exception handling allow us to do?  Where are exceptions handled?  What does exception handling facilitate?
Lecture 28 More on Exceptions COMP1681 / SE15 Introduction to Programming.
11-Jun-15 Exceptions. 2 Errors and Exceptions An error is a bug in your program dividing by zero going outside the bounds of an array trying to use a.
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.
16-Jun-15 Exceptions. Errors and Exceptions An error is a bug in your program dividing by zero going outside the bounds of an array trying to use a null.
Exceptions in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Exceptions. Errors and Exceptions An error is a bug in your program –dividing by zero –going outside the bounds of an array –trying to use a null reference.
Java Exceptions. Intro to Exceptions  What are exceptions? –Events that occur during the execution of a program that interrupt the normal flow of control.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. COMPSCI 125 Spring 2005 Chapter 8  Errors and Exceptions Throwable class.
Java Review 2 – Errors, Exceptions, Debugging Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Java Exceptions. Intro to Exceptions  What are exceptions? –Events that occur during the execution of a program that interrupt the normal flow of control.
Exceptions. Many problems in code are handled when the code is compiled, but not all Some are impossible to catch before the program is run  Must run.
What is an exception? An exception is: – an event that interrupts the normal processing of the program. –an error condition that violates the semantic.
Exception Handling. Exceptions and Errors When a problem encounters and unexpected termination or fault, it is called an exception When we try and divide.
MT311 Java Application Development and Programming Languages Li Tak Sing( 李德成 )
CIS 270—Application Development II Chapter 13—Exception Handling.
Exception Handling in Java Exception Handling Introduction: After completing this chapter, you will be able to comprehend the nature and kinds.
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.
Exceptions Syntax, semantics, and pragmatics Exceptions1.
COMP Exception Handling Yi Hong June 10, 2015.
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.
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.
1 Exception handling in Java Reading for this lecture: Weiss, Section 2.5 (exception handling), p. 47. ProgramLive, chapter 10. I need to know whether.
Sheet 3 HANDLING EXCEPTIONS Advanced Programming using Java By Nora Alaqeel.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 11 Handling Exceptions and Events.
Exceptions Chapter 16 This chapter explains: What as exception is Why they are useful Java exception facilities.
Exceptions and Assertions Chapter 15 – CSCI 1302.
CSE 332: C++ Statements C++ Statements In C++ statements are basic units of execution –Each ends with ; (can use expressions to compute values) –Statements.
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.
ICS3U_FileIO.ppt File Input/Output (I/O)‏ ICS3U_FileIO.ppt File I/O Declare a file object File myFile = new File("billy.txt"); a file object whose name.
Copyright © Curt Hill Error Handling in Java Throwing and catching exceptions.
Chapter 8-Exception Handling/ Robust Programming.
1 Exceptions. 2 Syntax Errors, Runtime Errors, and Logic Errors syntax errors, runtime errors, and logic errors You learned that there are three categories.
Java Programming: Exceptions1 Exceptions Reference: java.sun.com/docs/books/tutorial/essential/exceptions/
(c) University of Washington10-1 CSC 143 Java Errors and Exceptions Reading: Ch. 15.
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.
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.
Winter 2006CISC121 - Prof. McLeod1 Last Time Reviewed class structure: –attributes –methods –(inner classes) Looked at the effects of the modifiers: –public.
CSE 332: C++ Exceptions Motivation for C++ Exceptions Void Number:: operator/= (const double denom) { if (denom == 0.0) { // what to do here? } m_value.
Exceptions in the Java programming language J. W. Rider.
Exceptions 10-Nov-18.
Exceptions 10-Nov-18.
Exception Handling Chapter 9 Edited by JJ.
Exceptions 19-Feb-19.
Exceptions 25-Apr-19.
Exceptions 22-Apr-19.
CSC 143 Java Errors and Exceptions.
Exceptions 10-May-19.
Java Basics Exception Handling.
Exceptions 5-Jul-19.
Exception Handling.
Presentation transcript:

Introduction to Exceptions in Java CS201, SW Development Methods

Failures at run-time Divide by zero? Try to access an object reference that’s null? –Your program halts –It prints the location of where it failed You might think this is “built-in” as part of how Java runs a program But Java gives you the same ability to recognize error or situations and respond however you want to

When Things Go Wrong Java uses exceptions. When something “goes wrong” at a line of code: –An exception object gets created –Flow of control changes to some place in your code that can handle the exception Note: usually changes in flow-of-control are clearly marked by Java keywords –return, if/else, while, for, switch, call to a method, break, continue –Exceptions and a “jump” to somewhere else can occur where it’s not so obvious

What We Need to Know We need to understand –What an exception object is and what we might do with it. –How to manage the flow-of-control when an exception occurs Can we ignore exceptions? Some of them: –Unchecked exceptions: serious system problems, the program fails, you normally don’t try to handle these –Checked exceptions: Java requires you to do something with these. Either: Handle them when they occur Declare that you are passing responsibility back up to the method that called your code

Exception class hierarchy Java developers create a new exception class for each “type” of exception that can occur. A few examples: –NullPointerException: attempt to access an object reference that is null –IOException: some sort of I/O problem has occurred –FileNotFoundException: failed to open a file Each one its own class, all derived from the Exception superclass –You can easily create your own exceptions by extending Exception (but we won’t today)

Serious problems you shouldn’t try to worry about! Superclass of all Errors and Exceptions Problems we must catch We could catch this or any of its subclasses

How to Use an Exception Object Something bad has happened, and you have access to the exception object. What methods can you call on it? –String getMessage() Each exception encapsulates a text-message that says what the problem is –void printStackTrace() You’ve see this! It’s a list of the methods called that led to this exception, with the class and line numbers.

Control Flow and Exceptions Java lets you surround code that might generate an exception… –Called a “try block” And attach handlers that will take care of problems if they occur in this block –Called “catch blocks” Java also lets you do something else: –Declare that your method will pass any exception back up to it’s caller –We’ll explain later

try/catch/finally try { // lines of code that // might throw exceptions } catch ( ExceptionType1 e) { // handle 1 st type of exceptions } catch ( ExceptionType2 e) { // handle 2 nd type of exceptions } finally { // code executed in all cases } If some code in here throws an exception, then an exception object is created and we jump to the first catch block. Can have 1 or more catch- blocks. The first one where the thrown exception object matches the “parameter type” is executed. If we need to make sure something is always done after try-block and/or catch- block, it goes here

Comments on try/catch/finally try block –Any variable declared here is local to the try-block –Often, declare and initialize outside the try-block, then change it inside the block. catch blocks –Often there’s just one kind of exception that can happen. –If more than one possible Often we test for specific types of exceptions first Then have catch (Exception e) which will capture any other kind of exception since they all match the super-class type

Comments on try/catch/finally finally block –Always executed! whether or not an exception is thrown even if the try or catch blocks execute a return –Useful for closing files, freeing resources like database connections, etc.

What to Do in a Catch Block? You can do anything to try to recover from the problem –Ask for new input, use a different value, whatever makes sense –Print more useful information Remember getMessage() and printStackTrackTrace() can be called on the exception object –(To be explained later) You can throw the same exception which passes it back to the caller

Advice on Writing Exception Handling Code Empty catch blocks are used to ignore exceptions –Don’t do this unless it makes sense for your code! Keep it simple: wrap as few lines as possible in a try-block Recognize you can’t always safely recover –Sometimes best to “die gracefully”