Java Exceptions a quick review….

Slides:



Advertisements
Similar presentations
Exception Handling and Event Handling
Advertisements

Exceptions Chapter Throwing and Catching Exceptions When a program runs into a problem that it cannot handle, it throws an exception. Exceptions.
Exceptions: when things go wrong. Various sources of error public static doSomething() { int i = 3.0; while(!done); { int i = false } ) Syntactic errors.
Lecture 23 Input and output with files –(Sections 2.13, 8.7, 8.8) Exceptions and exception handling –(Chapter 17)
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 13 Exception Handling.
SE-1020 Dr. Mark L. Hornick 1 More Exception Handling and Throwing Exceptions.
1 Copyright © 1998 by Addison Wesley Longman, Inc. Chapter 13 In a language without exception handling: When an exception occurs, control goes to the operating.
MIT-AITI Lecture 14: Exceptions Handling Errors with Exceptions Kenya 2005.
Exceptions Any number of exceptional circumstances may arise during program execution that cause trouble import java.io.*; class IOExample { public static.
Chapter 14 Exception Handling and Event Handling.
COP 2800 Lake Sumter State College Mark Wilson, Instructor.
CSI 3120, Exception handling, page 1 Exception and Event Handling Credits Robert W. Sebesta, Concepts of Programming Languages, 8 th ed., 2007 Dr. Nathalie.
EXCEPTIONS Def: An exception is a run-time error. Examples include: attempting to divide by zero, or manipulate invalid data.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 8 Exception Handling Sections 1-5, 7.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 17 Exceptions and.
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.
ISBN Chapter 14 Exception Handling and Event Handling.
Exceptions in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 8 Exception Handling Sections 1-5, 7.
12.1 Exceptions The limitations of traditional methods of exception handling Error conditions are a certainty in programming Programmers make.
Introduction to Java Chapter 11 Error Handling. Motivations When a program runs into a runtime error, the program terminates abnormally. How can you handle.
1 Exception and Event Handling (Based on:Concepts of Programming Languages, 8 th edition, by Robert W. Sebesta, 2007)
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.
Java Software Solutions Foundations of Program Design Sixth Edition
Preventing and Correcting Errors
1 Exception Handling Introduction to Exception Handling Exception Handling in PLs –Ada –C++ –Java Sebesta Chapter 14.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 18 Exception Handling.
May 21, ICE 1341 – Programming Languages (Lecture #21) In-Young Ko Programming Languages (ICE 1341) Lecture #21 Programming Languages (ICE 1341)
Chapter 14 Exception Handling and Event Handling.
ISBN Chapter 14 Exception Handling and Event Handling.
06 Exception Handling. 2 Contents What is an Exception? Exception-handling in Java Types of Exceptions Exception Hierarchy try-catch()-finally Statement.
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.
And other languages…. must remember to check return value OR, must pass label/exception handler to every function Caller Function return status Caller.
Slides Credit Umair Javed LUMS Web Application Development.
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.
ISBN Chapter 14 Exception Handling and Event Handling.
BIO Java 1 Exception Handling Aborting program not always a good idea – can’t lose messages – E-commerce: must ensure correct handling of private.
ICS 313: Programming Language Theory Chapter 14: Exceptions.
Exceptions and Assertions Chapter 15 – CSCI 1302.
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.
ISBN Chapter 14 Exception Handling and Event Handling.
1 Exceptions. 2 Syntax Errors, Runtime Errors, and Logic Errors syntax errors, runtime errors, and logic errors You learned that there are three categories.
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.
Throw, Throws & Try-Catch Statements Explanations and Pictures from: Reference:
ECE122 L23: Exceptions December 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 24 Exceptions.
Lec.11 (Chapter 11) Exception Jiang (Jen) ZHENG July 13 th, 2005.
Exception Handling and Event Handling
Exception Handling and Event Handling
Exception and Event Handling
Chapter 13 Exception Handling
Exception Handling and Event Handling
Introduction to Exceptions in Java
ATS Application Programming: Java Programming
Chapter 12 Exception Handling
Exception Handling and Event Handling
Exception Handling and Reading / Writing Files
Web Design & Development Lecture 7
CSE 143 Java Exceptions 1/18/2019.
Exception Handling Imran Rashid CTO at ManiWeber Technologies.
Exception Handling and Event Handling
Exception Handling and Event Handling
Exception and Event Handling
Exception Handling and Event Handling
Java Basics Exception Handling.
CMSC 202 Exceptions 2nd Lecture.
Exception Handling and Event Handling
Exception Handling.
Presentation transcript:

Java Exceptions a quick review…

What happened before exceptions? must remember to check return value OR, must pass label/exception handler to every function Function Caller return status label to subprogram Function Caller Fortran strategy

Terminology Review An exception is an unusual event detectable by either hardware or software An exception is raised/thrown when its associated event occurs The exception handler code determines what to do

Advantages of exceptions Error detection code is tedious to write and it clutters the program Exception propagation allows a high level of reuse of exception handling code Encourage programmers to consider all events that could occur that might need to be handled – especially checked exceptions.

Exception Propagation main main calls function a a calls function b exception occurs in b If no try/catch in b, exception propagates to a If no try/catch in a, exception propagates to main If no try/catch in main, execution terminates (in C++, can override default handler, named unexpected) Continuation. If exception is handled, execution continues after last catch (unless handler calls exit of course). call a call b Problem! Both Java and C++ propagate exceptions

Java Exception Handling The Java library includes two subclasses of Throwable: Error Thrown by the Java interpreter for events such as heap overflow Never handled by user programs Exception User-defined exceptions are usually subclasses of this Has two predefined subclasses, IOException and RuntimeException (e.g., ArrayIndexOutOfBoundsException and NullPointerException)

Checked and Unchecked (review) Exceptions of class Error and RunTimeException and all of their descendants are called unchecked exceptions All other exceptions are called checked exceptions (compiler checks that your code handles – all exceptions occur at runtime) Methods containing code that can throw a checked exception must either: List the exception(s) in a throws clause, or Handle the exception (e.g., with try/catch) Example throws clause (not the same as throwing an exception!): public void myFunction() throws Exception

Checked miconceptions “Checked” means that Java checks that you know a line of code can generate an error The compiler does NOT know that an exception will occur… it knows that an exception might occur! ALL exceptions occur at RUNTIME

Java Details A method that calls a method with a checked exception in its throws clause has three alternatives: Catch and handle the exception Catch the exception and throw an exception that is listed in its own throws clause Declare that exception in its throws clause and do not handle it public void A() throws BadException { … } public void B() { … try { A() } catch (BadException e) { … } } public void C() throws AnException{ throw (AnException) } public void D() throws BadException { … A() … }

Imagine if NullPointer was checked Point p; Point p2 = new Point(3,4); try { int x = p.getX(); } catch (NullPointerException e) { syso(“there’s a logic but here”); } // compiler doesn’t execute… so even though // we know p2 is OK, the compiler wouldn’t int x = p2.getX();

Overriding methods A method cannot declare more exceptions in its throws clause than the method it overrides. Example: If you override run in a Thread class, you cannot add a throws clause.

Java Finally Can appear at the end of a try construct Form: ... } Purpose: To specify code that is to be executed, regardless of what happens in the try construct (e.g., to close file or database connection). Finally is executed even if there is a return statement in the block. http://stackoverflow.com/questions/161177/does-c-support-finally-blocks-and-whats-this-raii-i-keep-hearing-about

Finally example try { for (index = 0; index < 100; index++) { … if (…) { return; } //** end of if } //** end of for loop } //** end of try clause finally { } //** end of try construct * Java exercise has examples to play with