Exceptions1 Syntax, semantics, and pragmatics. Exception create If (some error){ throw new SomeException(”some message”); } Exceptions2.

Slides:



Advertisements
Similar presentations
Chapter 17 Failures and exceptions. This chapter discusses n Failure. n The meaning of system failure. n Causes of failure. n Handling failure. n Exception.
Advertisements

Exceptions CSE301 University of Sunderland Harry Erwin, PhD.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Yoshi
CMSC 202 Exceptions 2 nd Lecture. Aug 7, Methods may fail for multiple reasons public class BankAccount { private int balance = 0, minDeposit =
Lecture 23 Input and output with files –(Sections 2.13, 8.7, 8.8) Exceptions and exception handling –(Chapter 17)
COMP 121 Week 5: Exceptions and Exception Handling.
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.
Exceptions1 Syntax, semantics, and pragmatics. Exceptions2 Syntax, semantics, pragmatics Syntax –How it looks, i.e. how we have to program to satisfy.
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 and Exception Handling Carl Alphonce CSE116.
Exceptions1 Syntax, semantics, and pragmatics. Exceptions2 Syntax, semantics, pragmatics Syntax –How it looks, i.e. how we have to program to satisfy.
Lecture 28 More on Exceptions COMP1681 / SE15 Introduction to Programming.
Exceptions and Exception Handling (2) Carl Alphonce CSE116.
Exceptions and Exception Handling (continued) Carl Alphonce CSE116.
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.
1 Why do we need exceptions? In C, return variables must be used to indicate errors: if((fd = fopen(path,...)) == -1){ if(errno==a){...} else if(errno==b){...}
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.
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.
Advanced Java Course Exception Handling. Throwables Class Throwable has two subclasses: –Error So bad that you never even think about trying to catch.
Introduction to Java Chapter 11 Error Handling. Motivations When a program runs into a runtime error, the program terminates abnormally. How can you handle.
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.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 18 Exception Handling.
1 Exceptions and error handling. 2 Java exception mechanism when an error or exceptional condition occurs, you throw an exception which is caught by an.
Exception Handling. Definition  The term exception is shorthand for the phrase "exceptional event.“  An exception is an event, which occurs during the.
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.
07 Coding Conventions. 2 Demonstrate Developing Local Variables Describe Separating Public and Private Members during Declaration Explore Using System.exit.
Program documentation using the Javadoc tool 1 Program documentation Using the Javadoc tool.
Exceptions Syntax, semantics, and pragmatics Exceptions1.
COMP Exception Handling Yi Hong June 10, 2015.
Introduction to Exception Handling and Defensive Programming.
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.
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.
Programming & Debugging. Key Programming Issues Modularity Modifiability Ease of Use Fail-safe programming Style Debugging.
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.
(c) University of Washington10-1 CSC 143 Java Errors and Exceptions Reading: Ch. 15.
Effective Java, Chapter 9: Exceptions Items Last modified Fall 2012 Paul Ammann.
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.
Exceptions an unusual condition – e.g. division by zero – e.g. file doesn't exist – e.g. illegal type – etc. etc… typically a run-time error – i.e. during.
Introduction to Exceptions in Java CS201, SW Development Methods.
Lecture 5: Exception Handling and Text File I/O Michael Hsu CSULA.
Syntax, semantics, and pragmatics
Exception Handling.
CMSC 202 Exceptions 2nd Lecture.
Effective Java, 3rd Edition Chapter 10: Exceptions
CMSC 202 Exceptions 2nd Lecture.
CSE 143 Java Exceptions 1/18/2019.
CSC 143 Java Errors and Exceptions.
SWE 619 Last modified Fall 2007 Saket Kaushik, Paul Ammann
Effective Java, Chapter 9: Exceptions
Exceptions 10-May-19.
Why do we need exceptions?
Exceptions References: Jacquie Barker, Beginning Java Objects; Rick Mercer, Computing Fundamentals With Java; Wirfs-Brock et. al., Martin Fowler, OOPSLA.
CMSC 202 Exceptions 2nd Lecture.
Exception Handling.
Presentation transcript:

Exceptions1 Syntax, semantics, and pragmatics

Exception create If (some error){ throw new SomeException(”some message”); } Exceptions2

Exceptions catch Try{ methods where exception can occur } catch( SomeException se){ do the exception handling } Exceptions3

4 Finally The finally block is executed whether or not an exception is thrown. –Leaving the method you always execute the finally block Used to release resources –Example: Closing a connection to a network, database, or file –Coding idiom: FileReader input = null; try { … open input and use it … } finally { if (input != null) { input.close(); } }

Try with resource statement Java 7 language feature The coding idiom –Declare … try { open + use } finally {close } –Is now supported in the Java programming language New syntax –Try (open + initialize) { use } –The resource must implement the java.lang.AutoCloseable interface –Finally is no longer necessary in this case –Further readings + examples The Java Tutorial – ResourceClose.html Exceptions5

6 Program your own exception Why? –Technical exceptions like IOException, SQLException, etc. should not be propagated to the model layer. –Instead you must define your own application specific exception like LibraryException How? That’s very easy! –Define a new class which extends the class Exception –You probably need to define 3 constructors. –Your exception class may have data + methods But you probably never need it. –NetBeans can assist you.

Exceptions7 Item 57: Use exceptions only for exceptional conditions Don’t loop over a collection until it throws an exception. Only throw exceptions if the state is really exceptional –Searching for something without finding it, is that exceptional? Probably not. Better to return null.

Exceptions8 Item 58: Checked exceptions vs. run-time exceptions Use checked exceptions for recoverable conditions and run-time exceptions for programming errors –Use checked exception for conditions form which the call can reasonably be expected to recover. –Use run-time exceptions to indicate programming error The caller made an error –Most likely a violation the methods precondition –Examples: IndexOutOfBoundException, NullPointerException

Exceptions9 Item 59: Avoid unnecessary use of checked exceptions If the caller cannot handle the exception, then throw a run-time exception. Provide check methods –Example: StringTokenizer.hasMoreElements()

Exceptions10 Item 60: Favor the use of standard exceptions Don’t use a home-made exception if you can use a standard exception. Specially with run-time exceptions. Reusable standard run-time exceptions –IllegalArgumentException –IllegalStateException –NullPointerException –IndexOutOfBoundsException –UnsupporteOperationException

Exceptions11 Item 61: Throw exceptions appropriate to the abstraction Higher layers should catch lower-level exceptions and throw exceptions appropriate for the higher level Exception translation –Catch (LowLevelException ex) { throw new HighLevelException(message); } Exception chaining –Catch (LowLevelException ex) { throw new HighLevelException(ex); } –The LowLevelException is “inside” the HighLevelException –New in Java 1.4: New constructor in class Throwable

Exceptions12 Item 62: Document all exceptions thrown by each method For all your methods –Document (using the tag) all the exceptions the method might throw –Including unchecked exceptions. NetBeans can assist you –Mainly with checked exceptions. –Don’t forget the run-time exceptions.

Exceptions13 Item 63: Include failure-capture information in detail message The message in the exception is the only information the receiver gets. The message in the exception should include all values that “contributed” to the exception

Exceptions14 Item 64: Strive for failure atomicity A failed method should invocation should leave the object in the state that it was prior to invocation. –Easier to recover from exception.

Exceptions15 Item 65: Don’t ignore exceptions An empty catch block is highly suspicious –If you really mean it, then write a comment in the empty catch block.

Exceptions16 References Ken Arnold et al.: The Java Programming Language, 4 th edition, Addison Wesley, 2006 –Chapter 12: Exceptions and Assertions, page Joshua Bloch: Effective Java, 2nd edition, Addison Wesley, 2008 –Chapter 9: Exceptions, page