Fall 2017 CISC124 9/21/2018 CISC124 First onQ quiz this week – write in lab. More details in last Wednesday’s lecture. Repeated: The quiz availability.

Slides:



Advertisements
Similar presentations
Today Quiz solutions are posted on the Grading page. Assignment 1 due today, 7pm. Arrays as Pointers, Cont. Aliasing & Passing by Reference null Winter.
Advertisements

Lecture 23 Input and output with files –(Sections 2.13, 8.7, 8.8) Exceptions and exception handling –(Chapter 17)
Today Quiz solutions are posted on the Grading page. Assignment 2 is posted. Due the first Friday after Reading Week. All about null Start File Input/Output.
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.
1 Lecture#8: EXCEPTION HANDLING Overview l What exceptions should be handled or thrown ? l The syntax of the try statement. l The semantics of the try.
Object Oriented Programming
MT311 Java Application Development and Programming Languages Li Tak Sing( 李德成 )
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.
Fall 2015CISC124 - Prof. McLeod1 CISC124 Have you filled out the lab section survey? (As of last night 54 left to fill out the survey.) TA names have been.
Exceptions and Assertions Chapter 15 – CSCI 1302.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Today: –Review declaration, implementation, simple class structure. –Add an exception class and show.
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.
Lecture10 Exception Handling Jaeki Song. Introduction Categories of errors –Compilation error The rules of language have not been followed –Runtime error.
Exception. Agenda Exception. Handling Exceptions. The finally Clause.
Introduction to Exceptions in Java CS201, SW Development Methods.
Winter 2006CISC121 - Prof. McLeod1 Last Time Reviewed class structure: –attributes –methods –(inner classes) Looked at the effects of the modifiers: –public.
Lecture 5: Exception Handling and Text File I/O Michael Hsu CSULA.
Today… StringTokenizer class. Method Overloading. Catching Exceptions (and what they are!). Start Pointers and Aliasing. Winter 2016CMPE212 - Prof. McLeod1.
Object Throwable ErrorException RuntimeException.
Eighth Lecture Exception Handling in Java
Generics, Exceptions and Undo Command
Last Time Math class Style and Documentation Loops And Exercises!
Java Programming Language
CISC124 Labs start this week in JEFF 155: Meet your TA.
CS102 – Exceptions David Davenport Latest: May 2015
CSE 501N Fall ’09 17: Exception Handling
Fall 2017 CISC124 9/18/2018 CISC124 First onQ quiz this week – write in lab. More details in last Wednesday’s lecture. Repeated: The quiz availability.
Winter 2018 CMPE212 9/21/2018 CMPE212 – Stuff…
CMPE212 – Stuff… Exercises 4, 5 and 6 are all fair game now.
Exceptions 10-Nov-18.
Exceptions 10-Nov-18.
Exception Handling Chapter 9.
CISC124 Assignment 4 on Inheritance due next Monday, the 12th at 7pm.
Java Programming Language
CISC124 Assignment 4 on Inheritance due next Monday, the 12th at 7pm.
CISC124 Assignment 4 on Inheritance due next Monday, the 12th at 7pm.
Exception Handling and Reading / Writing Files
Fall 2018 CISC124 12/3/2018 CISC124 or talk to your grader with questions about assignment grading. Fall 2018 CISC124 - Prof. McLeod Prof. Alan McLeod.
Web Design & Development Lecture 7
CISC/CMPE320 - Prof. McLeod
Fall 2018 CISC124 2/15/2019 CISC124 TA names and s will be added to the course web site by the end of the week. Labs start next week in JEFF 155:
CISC124 Labs start this week in JEFF 155. Fall 2018
Fall 2018 CISC124 2/22/2019 CISC124 Quiz 1 This Week. Topics and format of quiz in last Tuesday’s notes. The prof. (me!) will start grading the quiz.
CISC/CMPE320 - Prof. McLeod
Fall 2018 CISC124 2/24/2019 CISC124 Quiz 1 marking is complete. Quiz average was about 40/60 or 67%. TAs are still grading assn 1. Assn 2 due this Friday,
Lecture 11 Objectives Learn what an exception is.
CMPE212 – Reminders Assignment 3 due next Friday.
CISC101 Reminders Assignment 3 due next Friday. Winter 2019
CMSC 202 Exceptions 2nd Lecture.
Winter 2019 CMPE212 4/17/2019 CMPE212 – Reminders
CMPE212 – Reminders Course Web Site:
Exceptions 25-Apr-19.
Last Time Some discussion of program “efficiency”. Arrays – 1D and 2D
Errors and Exceptions Error Errors are the wrongs that can make a program to go wrong. An error may produce an incorrect output or may terminate the execution.
Exceptions 22-Apr-19.
Winter 2019 CISC101 4/28/2019 CISC101 Reminders
CMPE212 – Reminders Quiz 1 marking done. Assignment 2 due next Friday.
Exception Handling Contents
Winter 2019 CMPE212 5/3/2019 CMPE212 – Reminders
Winter 2019 CMPE212 5/10/2019 CMPE212 – Reminders
Exceptions 10-May-19.
CISC101 Reminders Assignment 3 due today.
CMPE212 – Reminders Assignment 2 due next Friday.
CMPE212 – Reminders Assignment 4 on Inheritance due next Friday.
Java Basics Exception Handling.
Exceptions 5-Jul-19.
CMSC 202 Exceptions 2nd Lecture.
Exception Handling.
Presentation transcript:

Fall 2017 CISC124 9/21/2018 CISC124 First onQ quiz this week – write in lab. More details in last Wednesday’s lecture. Repeated: The quiz availability times will change to match each lab as the week progresses. Fall 2017 CISC124 - Prof. McLeod Prof. Alan McLeod

Today Exceptions Using Arrays to Model Pointers Aliasing Objects Fall 2017 CISC124 - Prof. McLeod

Fall 2013 CISC124 Exceptions How can a method indicate that it is unable to return what it is supposed to return? How can a method deliver details about the error condition? How else can you prevent the instantiation of an Object? The limitation of only returning a single “thing” means that you either designate error values for the “thing” or you have some other way to return the indication of an error. Fall 2017 CISC124 - Prof. McLeod Prof. Alan McLeod

Exceptions - Cont. The designers of Java followed conventions used by many other OOP languages - they allowed for another way to get something out of a method. However, an exception is thrown, not returned. Exceptions are Objects (big surprise!). When an error condition is encountered, a method can throw an instance of a pre-defined exception Object. A method can throw several exceptions, one for each possible kind of error condition. Fall 2017 CISC124 - Prof. McLeod

Exceptions - Cont., Propagation If a method throws an exception, then that method is immediately halted and there is no need for any return value, even if the method is non-void. The invoking method then receives the exception – if it does not catch it, then it goes to the next invoking method – all the way to main, if necessary. Called “cascading”. Finally, if main does not catch the exception, your program crashes and a message is sent to the console window. Fall 2017 CISC124 - Prof. McLeod

Exceptions - Cont., Message Handler How does an Exception Object contain information about the error condition? The type of the Object: IOException NumberFormatException FileNotFoundException ArrayIndexOutOfBoundsException … So, the method should throw a relevant exception object. Turns out that exceptions can also carry a String message. Fall 2017 CISC124 - Prof. McLeod

Bearer of "Bad Tidings" Exceptions – Cont. An exception is just a: The exception itself cannot do anything about the problem. But it can act to stop your program! Fall 2017 CISC124 - Prof. McLeod

Exceptions - Cont., Catching This is done with a “try/catch” block (see the next slide for the syntax). The compiler will force you to use try/catch blocks when you invoke methods that throw exceptions. (Eclipse can help build a try/catch: Select the code to be surrounded. Right click and choose “Surround with”, then “Try/catch block. The wizard will automatically choose all the possible exceptions.) Fall 2017 CISC124 - Prof. McLeod

Exceptions – Cont. Syntax of a “try-catch block”: try { // block of statements that might // generate an exception } catch (exception_type identifer) { // block of statements }[ catch (exception_type identifer) { … }][ finally { }] Fall 2017 CISC124 - Prof. McLeod

Exceptions – Cont. You must have at least one “catch block” after the “try block” (otherwise the try block would be useless!) You can have many catch blocks, one for each exception you are trying to catch. The code in the “finally” block is always executed, whether an exception is thrown, caught, or not. Fall 2017 CISC124 - Prof. McLeod

Checked vs Unchecked Exceptions Checked exceptions must either be caught in a method or thrown from that method (using the throws clause in the method header). Examples: IOException, FileNotFoundException, ClassNotFoundException You should not catch an un-checked exception or an Error. Examples: OutOfMemoryError, StackOverflowError, VirtualMachineError Fall 2017 CISC124 - Prof. McLeod

Checked vs Unchecked Exceptions, Cont. The compiler will ensure that checked exceptions are handled properly. Unchecked exceptions occur only at runtime and the compiler does not care about them. Unchecked exceptions are all sub-classes of java.lang.Error Fall 2017 CISC124 - Prof. McLeod

Try With Resources This new syntax is very useful with Java 7 & 8’s improved file I/O syntax. More on this topic in exercise 4. Fall 2017 CISC124 - Prof. McLeod

Try With Resources – Cont. Syntax of a “try-with-resources block”: try (instantiation; instantiation; …) { // other statements that might // generate an exception }[ catch (exception_type identifer) { // block of statements }][ catch (exception_type identifer) { … }][ finally { }] Fall 2017 CISC124 - Prof. McLeod

Try With Resources – Cont. The instantiation(s) inside the set of ( ) immediately after the try keyword are declared resources that must be local to the try/catch block. Note that there is no ; at the end of the list and that there does not have to be any catch blocks. These resources must all implement the AutoCloseable interface, which means that the try block can close the resource when it is finished. Resources will be closed whether or not an exception is thrown because their scope is forced to be in the try block only. As a result, all use of the resource must also take place in the try block. Fall 2017 CISC124 - Prof. McLeod

One-Dimensional Arrays - Declaration Fall 2013 CISC124 One-Dimensional Arrays - Declaration As we have already seen - To create an array to hold 10 integers: int[] testArray = new int[10]; testArray now points to an area of memory that holds locations for 10 integers. It also points to one location that holds testArray.length which is an attribute of the array, that is equal to the number of elements. Arrays are Objects in Java. Fall 2017 CISC124 - Prof. McLeod Prof. Alan McLeod

Modelling Pointers Use the array as an example to help model the behaviour of a pointer. Note that an array is an example of a mutable object – object contents can be changed by accessing the object’s contents through the pointer. Object design or class design determines mutability. Fall 2017 CISC124 - Prof. McLeod

One-Dimensional Arrays - Declaration, Cont. 0480ff 0180ff … int[] testArray 0480ff As a “pointer”, testArray points to an area of memory that contains a series of int values as well as the attribute length. 10 .length Fall 2017 CISC124 - Prof. McLeod

One-Dimensional Arrays - Declaration, Cont. The java statement above can be split into two: int[] testArray; testArray = new int[10]; The first statement creates a variable of type int[] - that is to say that it will be an array of int’s. The variable, testArray is now an object of type int[] that contains an “object reference” or a pointer. The object reference is null after the first statement. Fall 2017 CISC124 - Prof. McLeod

A “Null Pointer” After int[] testArray; int[] testArray null 0180ff … Fall 2017 CISC124 - Prof. McLeod

After the new Keyword After testArray = new int[10]; int[] testArray 0480ff 0180ff … int[] testArray 0480ff 10 Fall 2017 CISC124 - Prof. McLeod

One-Dimensional Arrays - Cont. The array indices allow mutability. Memory locations are calculated by offsets from the base address: testArray[0] testArray[1] testArray[2] testArray[3] testArray[4] testArray[5] testArray[6] testArray[7] testArray[8] testArray[9] testArray.length 10 Array indices Fall 2017 CISC124 - Prof. McLeod

Multi-Dimensional Arrays Consider: int[][] exArray = new int[3][5]; 1002fc int[][] exArray 0480ff int[] exArray[0] 1002fc exArray[1] 1010fc exArray[2] 1201ab 1010fc 1201ab Fall 2017 CISC124 - Prof. McLeod

Multi-Dimensional Arrays - Cont. So exArray points to three one dimensional arrays: exArray[0] exArray[1] exArray[2] Each of these arrays has the same length: exArray[2].length // returns 5 Yes, you can refer to these arrays in code, just like this. Fall 2017 CISC124 - Prof. McLeod

Multi-Dimensional Arrays - Cont. int[][] twoDArray = new int[10][20]; The above is equivalent to: int[][] twoDArray; twoDArray = new int[10][]; for (int i = 0; i < 10; i++) twoDArray[i] = new int[20]; As shown above: twoDArray.length // gives 10 twoDArray[0].length // gives 20 Fall 2017 CISC124 - Prof. McLeod

Multi-Dimensional Arrays - Cont. “Ragged Arrays” are not “square”, and are legal in Java: int[][] raggedArray = new int[5][]; raggedArray[0] = new int[5]; raggedArray[1] = new int[3]; raggedArray[2] = new int[2]; raggedArray[3] = new int[8]; raggedArray[4] = new int[12]; Fall 2017 CISC124 - Prof. McLeod

Multi-Dimensional Arrays - Cont. Array initializer for two-D array, for example: int[][] twoDArray = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {10, 11, 12}}; System.out.println(twoDArray.length); // 4 System.out.println(twoDArray[0].length); // 3 Fall 2017 CISC124 - Prof. McLeod

Aliasing Objects - Array Example int[] first = {1, 2, 3, 4, 5}; int[] second = {10, 20, 30, 40, 50, 60, 70}; 1 2 3 4 5 10 20 30 40 50 60 70 0480ff 0960ff int[] first 0480ff int[] second 0960ff .length 5 .length 7 Fall 2017 CISC124 - Prof. McLeod

Aliasing Objects - Array Example, Cont. second = first; // Aliasing! 1 2 3 4 5 10 20 30 40 50 60 70 0480ff 0960ff int[] first 0480ff int[] second 0480ff .length 5 .length 7 Fall 2017 CISC124 - Prof. McLeod

Aliasing Objects - Array Example, Cont. // after garbage collection Poof! 1 2 3 4 5 0480ff int[] first 0480ff int[] second 0480ff .length 5 Fall 2017 CISC124 - Prof. McLeod

Aside – “Garbage Collection” in Java Some computer programming languages require you to indicate when you are done with variables so the memory they are occupying can be released back to the OS. Called “Garbage Collection”. (Fortunately!) Java has an automatic Garbage Collection system: Variables are garbage collected once you move outside their scope. Object contents are garbage collected when there are no pointers pointing to the contents. Fall 2017 CISC124 - Prof. McLeod

Aliasing Objects - Array Example, Cont. first[4] = 500; // second[4] is also 500 1 2 3 4 500 0480ff int[] first 0480ff int[] second 0480ff .length 5 Fall 2017 CISC124 - Prof. McLeod

Aliasing Objects - Array Example, Cont. Fall 2013 CISC124 Aliasing Objects - Array Example, Cont. So, setting one array to equal another as in: array1 = array2; sets array1 to point to the same data memory location that was (and still is) pointed to by array2. Now, changing the value of an element in array2 will change that same element in array1, or visa-versa - this makes sense since both array Objects point to the same set of data values in memory! Fall 2017 CISC124 - Prof. McLeod Prof. Alan McLeod

Aliasing Objects Passing an Object into a method results in the method’s parameter being aliased to the Object passed. Called “Passing by Reference”! Fall 2017 CISC124 - Prof. McLeod