Advanced Java Course Exception Handling. Throwables Class Throwable has two subclasses: –Error So bad that you never even think about trying to catch.

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

1 Java coding conventions Lecture 2. 2 Please read Not only on assignment 1, but always. 80% of the lifetime.
Exceptions Ensuring program reliability. Program correctness The term program correctness refers to a program’s working as advertised; that is, it produces.
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.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 15: Exception Handling.
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.
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.
Exception Handling 1 CISC6795, Spring Introduction 2 Exception – an indication of a problem that occurs during a program’s execution, for examples:
© 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.
Exceptions1 Syntax, semantics, and pragmatics. Exceptions2 Syntax, semantics, pragmatics Syntax –How it looks, i.e. how we have to program to satisfy.
COP INTERMEDIATE JAVA Exception Handling Serialization.
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.
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. 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.
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.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. COMPSCI 125 Spring 2005 Chapter 8  Errors and Exceptions Throwable class.
Slides prepared by Rose Williams, Binghamton University Chapter 9 More Exception Handling.
Java Exceptions. Intro to Exceptions  What are exceptions? –Events that occur during the execution of a program that interrupt the normal flow of control.
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.
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.
CIS 270—Application Development II Chapter 13—Exception Handling.
Chapter 12: Exception Handling
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.
Exceptions1 Syntax, semantics, and pragmatics. Exception create If (some error){ throw new SomeException(”some message”); } Exceptions2.
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…
Exceptions Handling Exceptionally Sticky Problems.
COMP Exception Handling Yi Hong June 10, 2015.
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.
Data Structures Using Java1 Chapter 2 Inheritance and Exception Handling.
Exceptions cs1043. Program Exceptions When a program detects an error, what should it do? – Nothing, simply allow the program to fail. – Implement a course.
Exception Handling Programmers must deal with errors and exceptional situations: User input errors Device errors Empty disk space, no memory Component.
Unit 4 School of Information Systems & Technology1 School of Information Systems and Technology (IST)
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.
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.
Exceptions Lecture 11 COMP 401, Fall /25/2014.
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.
Introduction to Exceptions in Java CS201, SW Development Methods.
Lecture 5: Exception Handling and Text File I/O Michael Hsu CSULA.
Java Exceptions a quick review….
Exceptions 10-Nov-18.
Advanced Java Programming
Exceptions Handling the unexpected
Chapter 12 Exception Handling
Exception Handling Chapter 9 Edited by JJ.
Web Design & Development Lecture 7
Chapter 13 Exception Handling
Exceptions 19-Feb-19.
Exceptions 7-Apr-19.
Exceptions 25-Apr-19.
Exceptions 22-Apr-19.
Exceptions 10-May-19.
Exceptions 5-Jul-19.
Exception Handling and Event Handling
Exception Handling.
Presentation transcript:

Advanced Java Course Exception Handling

Throwables Class Throwable has two subclasses: –Error So bad that you never even think about trying to catch these –Exception Most Exceptions must be handled (i.e. either declared in the method’s throws clause or caught) Except Runtime Exceptions, which are “unchecked” – they don’t have to be declared

Syntax of Exception Handling To throw: > >( >) throws > { > } To catch: > >( >) { try { > } catch ( > >) { > }

When to do what? If code that your code calls declares that it can throw an Exception, what should you do? –Just throw the exception –Catch the exception, report it, and recover –Catch the exception, make a new, different exception, and throw the new one When do you throw an Exception even if no Exception was thrown to you?

When to re-throw the Exception If it’s more appropriate to deal with it at a higher level of the code. If the exception means death to the method it’s in. In other words, there’s no reasonable way you can continue to execute the code of this method

When to catch it and recover If this is the appropriate place to report the exception to the user, or log it, or whatever you plan to do with it. If you can and should continue with this method – for example if you can use a default value, or try again.

When to catch it & throw your own You’re sure the Exception that must be handled is impossible (if your code is correct), so you don’t want to have to declare it all the way up. On the other hand, if your code is incorrect, you should not “hide” the exception – you need to see it so that you can fix your programmer error. Throw a new RuntimeException. You want to create an abstraction layer, and hide the implementation. Throw a new instantiation of your own Exception class.

When to create your own Exception Sometimes you might create a new Exception instance even though none of your code threw one to you. You might be the first “thrower” of this Exception. You should do this if your code detects some error state that you cannot recover from in this method – for example, an input might be of an illegal value.

Why you might need a new Exception subclass If you’re creating a new framework, or library, chances are you’ll want Exceptions that ‘fit’ within that framework, and are descriptive off it. This is part of creating that abstraction layer. You want added functionality, like the ability to wrap an Exception around another, so that you can create the abstraction layer without totally destroying the information of what’s below it.

Abstraction Layer? Abstraction Layer (mentioned in the previous slide) –When you implement a new Type, and you want to hide the implementation from the user of your new type, so that they don’t have to worry about how you did it. –Biology metaphor: my body produces an abstraction layer so that when I shake someone’s hand, they don’t have to worry about how I did it. –When you create a class Graph in Java, for example, the person using the Graph shouldn’t need to know whether you used Arrays or Vectors or something else to implement it. Creating your own Exceptions is part of this.

Checked or Unchecked? If you create your own new Exception class, should it be checked or Runtime? –RuntimeExceptions are for programmer error – i.e. your code is in a state it just oughtn’t be in, and it’s a really bad state. Stop program execution and go debug. By the time a program reaches an end-user, it should never throw these. The outermost layer of your program should gracefully catch all Exceptions, even Runtimes and do something user-friendly. –Checked exceptions (all Exceptions other than RuntimeExceptions) are for dealing with the outside world. If something about the input the user gave you is bad, or a file is corrupt, or a database server is down, these are real-life things that could happen, and your program should cope with them as gracefully as possible.

Summary of Exceptions Catch and hide an exception if –Can recover: (default value, user try again) Re-throw an exception if –Irrecoverable to method –More appropriate to handle elsewhere Throw new if –Building abstraction layer –Want to throw unchecked runtime exception

Summary of Exceptions When creating your own exception class –Name it descriptively –Place appropriately in class heirarchy –If programmer error, make it a subclass of RuntimeException (so it’s unchecked) –If outside error (network, user, etc.), make it a checked Exception