Exception Handling Generics. Amit Gupta

Similar documents
COMP1008 Exceptions. Runtime Error

Day 8. COMP1006/1406 Summer M. Jason Hinek Carleton University

Object oriented programming. Instructor: Masoud Asghari Web page: Ch: 7

Lecture 20. Java Exceptional Event Handling. Dr. Martin O Connor CA166

Exceptions in Java

Exception-Handling Overview

PIC 20A Exceptions. Ernest Ryu UCLA Mathematics. Last edited: November 27, 2017

Inheritance and Encapsulation. Amit Gupta

Exceptions. CSC207 Winter 2017

CS 61B Data Structures and Programming Methodology. July 7, 2008 David Sun

Exceptions. References. Exceptions. Exceptional Conditions. CSE 413, Autumn 2005 Programming Languages

Exceptions. CSE 142, Summer 2002 Computer Programming 1.

Exceptions. Readings and References. Exceptions. Exceptional Conditions. Reading. CSE 142, Summer 2002 Computer Programming 1.

BBM 102 Introduction to Programming II Spring Exceptions

CSE 331 Software Design & Implementation

6.Introducing Classes 9. Exceptions

CS 3 Introduction to Software Engineering. 3: Exceptions

CSC207H: Software Design. Exceptions. CSC207 Winter 2018

BBM 102 Introduction to Programming II Spring 2017

Exceptions, try - catch - finally, throws keyword. JAVA Standard Edition

Checked and Unchecked Exceptions in Java

Chapter 13 Exception Handling

Assoc. Prof. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

CS112 Lecture: Exceptions. Objectives: 1. Introduce the concepts of program robustness and reliability 2. Introduce exceptions

Research on the Novel and Efficient Mechanism of Exception Handling Techniques for Java. Xiaoqing Lv 1 1 huihua College Of Hebei Normal University,

Internal Classes and Exceptions

Exception Handling. Chapter 9

Programming II (CS300)

A problem?... Exceptions. A problem?... A problem?... Suggested Reading: Bruce Eckel, Thinking in Java (Fourth Edition) Error Handling with Exceptions

Exceptions. Examples of code which shows the syntax and all that

Comp 249 Programming Methodology Chapter 9 Exception Handling

Lecture 10 Assertions & Exceptions

CS365 Midterm -- Spring 2019

Exception Handling. General idea Checked vs. unchecked exceptions Semantics of... Example from text: DataAnalyzer.

Object Oriented Programming

Sri Vidya College of Engineering & Technology Question Bank

Lecture 12 Assertions & Exceptions

Contents. I Introductory Examples. Topic 06 -Exception Handling

Exceptions Binary files Sequential/Random access Serializing objects

Exception Handling. Sometimes when the computer tries to execute a statement something goes wrong:

Inheritance. SOTE notebook. November 06, n Unidirectional association. Inheritance ("extends") Use relationship

Exception Handling. Run-time Errors. Methods Failure. Sometimes when the computer tries to execute a statement something goes wrong:

14. Exception Handling

Exceptions. Computer Science and Engineering College of Engineering The Ohio State University. Lecture 15

Chapter 9. Exception Handling. Copyright 2016 Pearson Inc. All rights reserved.

abstract binary class composition diamond Error Exception executable extends friend generic hash implementation implements

Exceptions and Libraries

Fundamentals of Object Oriented Programming

Exceptions and assertions

CS 151. Exceptions & Javadoc. slides available on course website. Sunday, September 9, 12

Abstract Classes, Exceptions

COS226 - Spring 2018 Class Meeting # 21 April 23, 2018

Program Correctness and Efficiency. Chapter 2

CS-202 Introduction to Object Oriented Programming

Exceptions. Produced by. Introduction to the Java Programming Language. Eamonn de Leastar

CS159. Nathan Sprague

CS112 Lecture: Exceptions and Assertions

Introduction to Computation and Problem Solving. Class 25: Error Handling in Java. Prof. Steven R. Lerman and Dr. V. Judson Harward.

JAVA MOCK TEST JAVA MOCK TEST IV

Object Oriented Programming. Week 7 Part 1 Exceptions

Topic 6: Exceptions. Exceptions are a Java mechanism for dealing with errors & unusual situations

DOWNLOAD PDF CORE JAVA APTITUDE QUESTIONS AND ANSWERS

Object-Oriented Principles and Practice / C++

Le L c e t c ur u e e 5 To T p o i p c i s c t o o b e b e co c v o e v r e ed e Exception Handling

Data Structures. 02 Exception Handling

Exceptions. Produced by. Algorithms. Eamonn de Leastar Department of Computing, Maths & Physics Waterford Institute of Technology

National University. Faculty of Computer Since and Technology Object Oriented Programming

What can go wrong in a Java program while running?

CSE 331 Software Design and Implementation. Lecture 12 Assertions & Exceptions

Contents. Figures. Tables. Examples. Foreword. Preface. 1 Basics of Java Programming 1. xix. xxi. xxiii. xxvii. xxix

Administration. Exceptions. Leftovers. Agenda. When Things Go Wrong. Handling Errors. CS 99 Summer 2000 Michael Clarkson Lecture 11

Programming Languages and Techniques (CIS120)

Chapter 14. Exception Handling and Event Handling

Lecture 14: Exceptions 10:00 AM, Feb 26, 2018

Assertions and Exceptions Lecture 11 Fall 2005

Java Exceptions Version June 2009

Programming II (CS300)

Lab 5: Java IO 12:00 PM, Feb 21, 2018

Full file at Chapter 2 - Inheritance and Exception Handling

Exceptions. When do we need exception mechanism? When and why use exceptions? The purpose of exceptions

COMP200 EXCEPTIONS. OOP using Java, based on slides by Shayan Javed

Chapter 8. Exception Handling. CS 180 Sunil Prabhakar Department of Computer Science Purdue University

CSE 331 Software Design and Implementation. Lecture 12 Assertions & Exceptions

Object Oriented Programming

Inf1-OP. Inf1-OP Exam Review. Timothy Hospedales, adapting earlier version by Perdita Stevens and Ewan Klein. March 20, School of Informatics

An Evolvable Model and Tool Support for Java Exception Handling. Y C Cheng

COE318 Lecture Notes Week 10 (Nov 7, 2011)

Software Design and Analysis for Engineers

What is the purpose of exceptions and exception handling? Vocabulary: throw/raise and catch/handle Exception propagation Java checked and unchecked

Objectives for this class meeting. 1. Conduct review of core concepts concerning contracts and pre/post conditions

CISC-124. Passing Parameters. A Java method cannot change the value of any of the arguments passed to its parameters.

Exceptions. What exceptional things might our programs run in to?

2.6 Error, exception and event handling

EXCEPTION HANDLING. Summer 2018

CS 520/620 Advanced Software Engineering Spring February 02, 2016

Preconditions. CMSC 330: Organization of Programming Languages. Signaling Errors. Dealing with Errors

Inf1-OOP. OOP Exam Review. Perdita Stevens, adapting earlier version by Ewan Klein. March 16, School of Informatics

CS193j, Stanford Handout #25. Exceptions

15CS45 : OBJECT ORIENTED CONCEPTS

Exception Handling: Control. Exception handling is the control of error conditions or other unusual events during the execution of a program.

Transcription:

Exception Handling Generics Amit Gupta

Announcements Project 2 deadline 18 th Feb 9 pm. TA Consulting hours Mon Thurs B146 6 9 pm Exam 1 : Feb 15 4:30 5:20 pm Project 1 grading

Exception Handling Computer programs are filled with errors. It may call other code which contain mistakes. We have to handle program errors. What mechanisms Java provides in order to deal with them?

Exception Handling You call a method in a class that you did not write That method does some risky, something that might not work at runtime. You should know that the method you are calling is RISKY.

Exception Handling Methods in Java use exceptions to tell the calling code that something bad happened. You then write code that can handle the failure if it does happen.

How to define a RISKY method throws keyword in Java declares that a method throws an exception. An exception is always thrown back to the caller.

Example: Java Documentation

Handling Exceptions One method will catch what another method throws. Calling Risky method code should be within try/catch block. Code for handling an exception is written within catch block.

What is an Exception?? An exception is an object of class Exception. All Exception classes extend Throwable and inherit two methods. getmessage(); getstacktrace();

Checked and Unchecked Exceptions Java Compiler checks for everything except Runtime Exception(Unchecked Exceptions). Most Runtime exceptions come from a problem in your code logic that fails at runtime in ways you cannot predict. You WANT Runtime Exceptions to happen at development time.

Flow Control in try/catch blocks If try succeeds If try fails

finally keyword A finally block is where you put code that must run regardless of an exception. Without finally, you have to put code in both the try and catch blocks.

Multiple Exceptions A method can throw multiple exceptions. It should define all the checked exceptions it can throw. The calling code must handle all the exceptions thrown by the method.

Polymorphic Exceptions Exceptions can be declared using a super type of the exceptions. It can catch any exception of Clothing Exception subclass. You can catch specific type of exceptions and handle each of them or catch a super type and handle them in a same way

Order of Multiple Catch Blocks Multiple catch blocks must be ordered from smallest to biggest. DON T DO THIS With catch blocks, JVM starts at first one and goes down until it finds a catch block that s broad enough.

Ducking an exception When you don t want to handle an exception, you can duck it by declaring it. Ducking(by declaring) only delays the inevitable. Sooner or later, somebody has to deal with it. If exception reaches main() and it also ducks it, JVM shuts down.

Handle or Declare. It s the LAW Handle Declare

Summary : Exception Handling Whenever an unusual situation is reached, an exception is thrown and normal execution stops immediately. The exception looks for code that is designed to deal with that specific exception. The code which handles exception can be in current method, in the calling method, or far back in a chain of method calls, all the way to main().

Summary : Exception Handling If no handling code is found, the exception will propagate all the way past main() to the JVM and the program will end. Exceptions provide a simple and unified way to handle errors. You can choose to deal with errors directly or delegate that responsibility to the methods that call the code you are writing.

Questions

Generics A generic class gives a template for creating classes in which a placeholder for the underlying data type can be filled in when a specific instance of that class is created. Same definition of MyGeneric is used with different underlying data types. If we omit the underlying type, the compiler will use Object as the underlying data type.

With Generics, we can create type safe collections where more problems are collected at compile time instead of runtime. Without generics, the compiler would let you put any object to an ArrayList that was supposed to hold Fish objects. Why Generics??

1. Creating instances of generic classes. 2. Declaring and assigning variables of generic classes. 3. Declaring and Invoking methods that take generic types. Using Generics

Defining Generic classes Defines a generic version of a linked list. T is used as a placeholder for different data types.

Questions

NO QUIZ TODAY

ALL THE BEST!