What/how do we care about a program?

Slides:



Advertisements
Similar presentations
Growth-rate Functions
Advertisements

Exceptions: when things go wrong. Various sources of error public static doSomething() { int i = 3.0; while(!done); { int i = false } ) Syntactic errors.
Yoshi
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.
SE-1020 Dr. Mark L. Hornick 1 More Exception Handling and Throwing Exceptions.
EXCEPTIONS. What’s an exception?? Change the flow of control when something important happens ideally - we catch errors at compile time doesn’t happen.
Exception Handling Chapter 12.  Errors- the various bugs, blunders, typos and other problems that stop a program from running successfully  Natural.
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.
Exceptions in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 Exception Handling  Introduction to Exceptions  How exceptions are generated  A partial hierarchy of Java exceptions  Checked and Unchecked Exceptions.
Introduction to Java Chapter 11 Error Handling. Motivations When a program runs into a runtime error, the program terminates abnormally. How can you handle.
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Exception Handling in Java Course Lecture Slides 7 th July 2010 “ Admitting.
CS203 Java Object Oriented Programming Errors and Exception Handling.
Exceptions 1. Your computer takes exception Exceptions are errors in the logic of a program (run-time errors). Examples: Exception in thread “main” java.io.FileNotFoundException:
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…
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.
What/how do we care about a program? Robustness Correctness Efficiency (speed, space) 11/2/20151IT 179  Software Testing  Error Handling  Efficiency.
Copyright © Curt Hill Error Handling in Java Throwing and catching exceptions.
1 Exceptions. 2 Syntax Errors, Runtime Errors, and Logic Errors syntax errors, runtime errors, and logic errors You learned that there are three categories.
Exception. Agenda Exception. Handling Exceptions. The finally Clause.
3/21/2016IT 2751 Tow kinds of Lists Array What can be done? What can be easily done? student 1 student 2 student 3 student 4 Linked List student 2 student.
Throw, Throws & Try-Catch Statements Explanations and Pictures from: Reference:
Exceptions and Error Handling. Exceptions Errors that occur during program execution We should try to ‘gracefully’ deal with the error Not like this.
Introduction to Exceptions in Java CS201, SW Development Methods.
Lecture 5: Exception Handling and Text File I/O Michael Hsu CSULA.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Data Structures in Java: From Abstract Data Types to the Java Collections.
Object Throwable ErrorException RuntimeException.
Chapter 2: Error Handling, Software Testing and Program Efficiency
Algorithm Analysis 1.
Java Exceptions a quick review….
Chapter 10 – Exception Handling
MIT AITI 2003 Lecture14 Exceptions
Chapter 13 Exception Handling
Introduction Exception handling Exception Handles errors
CS102 – Exceptions David Davenport Latest: May 2015
Creating and Modifying Text part 2
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.
Exceptions 10-Nov-18.
E x c e p t i o n s Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live. — Martin Golding.
EE422C Software Implementation II
Building Java Programs
Exception Handling Chapter 9.
Advanced Java Programming
Exceptions Handling the unexpected
Chapter 12 Exception Handling
Exception Handling and Reading / Writing Files
CS 201 Fundamental Structures of Computer Science
CMSC 202 Exceptions 2nd Lecture.
Web Design & Development Lecture 7
Building Java Programs
Chapter 13 Exception Handling
Chapter 12: Exceptions and Advanced File I/O
Building Java Programs
CMSC 202 Exceptions 2nd Lecture.
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.
Tutorial Exceptions Handling.
Chapter 12 Exception Handling and Text IO Part 1
Exception Handling Contents
Sum this up for me Let’s write a method to calculate the sum from 1 to some n public static int sum1(int n) { int sum = 0; for (int i = 1; i
E x c e p t i o n s Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live. — Martin Golding.
Tutorial MutliThreading.
Exceptions 10-May-19.
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.
Algorithms and data structures: basic definitions
Presentation transcript:

What/how do we care about a program? Robustness  Error Handling Correctness  Software Testing Efficiency (speed, space)  Algorithm Analysis 11/12/2018 IT 179

Things that can go wrong Logical error – The program is incorrect  Environmental error - e.g. out of memory I/O error – e.g., lost network connection. Let’s handle it!! Beyond programmer’s control 11/12/2018 IT 179

Exceptions In Java, they are classes Throwing Exceptions is Java’s way of telling you something has gone wrong When an “exceptional condition” occurs, an exception object is created storing information about the nature of the exception (kind, where it occurred, etc.). When this happens, we say that “an exception is thrown”. The JVM looks for a block of code to catch and handle the exception (do something with it) 11/12/2018 IT 179

Exception throwing bad thing happens The sequence of throwing exceptions method c  exception occurs X If the exception cannot be handled here, it will continue to throw back method b  method c() method a  method b() main() calls  method a() JVM starts application at main() The sequence of calling 11/12/2018 IT 179

Try-Catch-Finally Blocks program statements; some of which may throw an exception } catch ( ExceptionType1 exception ) { program statements to handle exceptions of type ExceptionType1 or any of its subclasses catch ( ExceptionType2 exception ) { program statements to handle exceptions of type ExceptionType2 … . . . other catch clauses catch ( ExceptionTypeN exception ) { program statements to handle exceptions of type ExceptionTypeN finally { this block is optional; this block will execute whether or not an exception is thrown

try { int i=1,j=2; Exception myEx=null; String s="0.2"; i=Integer.parseInt(s); if (i > j) throw new RuntimeException(); int[] A = new int[5]; i=1; if (i<2 || i>4) throw new IndexOutOfBoundsException(); i = 5; i = A[i]; } catch (ArithmeticException ex) { System.out.println("\n"+ex.toString()); ex.printStackTrace(); catch (NullPointerException ex) { System.out.println("I'm here 12345"); catch (NumberFormatException ex) { System.out.println("\n"+ex.toString()); ex.printStackTrace(); } catch (IllegalArgumentException ex) { catch (ArrayIndexOutOfBoundsException ex) { catch (IndexOutOfBoundsException ex) { catch (RuntimeException ex) { finally { System.out.println("\nWe are done. Finally!!"); 11/12/2018 IT 179

String fname; Scanner inS = null; fname = (args.length != 0 ? args[0] : "/home/ADILSTU/ccli/Public/IT179/exps.txt"); try { File file = new File(fname); inS = new Scanner(file); } catch (FileNotFoundException ex) { System.out.println("Can't find file: "+fname); System.exit(-1); 11/12/2018 IT 179

Categories of exceptions These categorization affect compile-time behavior only Checked exceptions – descended from class Exception, but outside the hierarchy rooted at RuntimeException. The compiler will check that you either catch or re-throw checked exceptions. Unchecked exceptions – (aka Runtime Exception ) represent the kinds of errors your program can avoid through careful programming and testing. The compile does not check to see that you handle these exceptions. These represent some error, not programmer’s fault, but the programmer can (should) do something about it They are programmer’s fault, the compiler can’t do a thing. 11/12/2018 IT 179

Unchecked Exceptions handle: Logical error – The program is incorrect  Environmental error - e.g. out of memory I/O error – e.g., lost network connection. Checked Exceptions handle: 11/12/2018 IT 179

Java’s Exception Hierarchy Unchecked Checked

Correctness  Software Testing You should “test” throughout the software development cycle, namely, every stages: After the analysis and design are completed During the implementation (test driven development) (Write a little, test a little) After the implementation is completed 11/12/2018 IT 179

Efficiency (speed, space)  Efficiency Analysis An algorithm’s performance can be measured by: Time complexity Space complexity How long it takes to execute. How much additional space the algorithm needs. (memory, storage, tape). 11/12/2018 IT 179

Time Complexity: Count Instructions Statements Cost 1 double average( int []a, int n ) { 2 double sum = 0; 3 int count = 0; 4 while ( count < n ) { n + 1 5 sum += a[count]; n 6 count++; 7 } 8 if ( n > 0 ) 9 return sum / n; 10 else 11 return 0.0f; 12 TOTAL 3n + 5 How many times will each instruction execute? 11/12/2018 IT 179

asymptotic approach  n2 n+(n-1)+(n-2)+......+3+2+1 = n(n+1)/2 for (int i=0; i< n; i++) { .......... for (int j=i; j < n; j++) { ............ } .........  n2 11/12/2018 IT 179

Consider the worst part of the program if (....) { ......... } else { n2  n3 n3 11/12/2018 IT 179

Big-O notation in Algorithm Analysis for (int i=0; i< n; i++) { ................ } .......... for (int j=0; j< n; j++) { ............ for (int k=0; k<n; k++) { ........... ......... Big-O notation in Algorithm Analysis  n  n3 O(c+n+n3+n2)  O(n3)  n2 11/12/2018 IT 179

Linear Search Statements Cost 1 int linearSearch( int [] a, int n, int target ) { 2 int i = 0; 3 while ( i < n ) { n + 1 4 if ( target == a[i] ) n 5 return i; 6 i++; 7 } 8 return –1; 9 TOTAL 3n + 3 11/12/2018 IT 179

O(n-s)=O(n) O(3) = O(1) // select the minimum between a[s] to the end // and return the index of the minimum // private int min(int a[], int s){ int m = s; for (int i=s; i < a.length;i++) { if (a[i] < a[m]) m=i; } return m; // exchange the contents of a[i] and a[j] private void exchange(int[] a, int i, int j) { int temp = a[i]; a[i] = a[j]; a[j] = temp; O(n-s)=O(n) where n is the size of the array O(3) = O(1) 11/12/2018 IT 179

public void sort(int a[]) { // select right element for a[i] for (int i = 0; i<a.length-1; i++) { int j = min(a,i); exchange(a,i,j); } Selection Sort (a quadratic sort) O(n2) 9 6 3 4 2 8 5 2 6 3 4 9 8 5 2 3 6 4 9 8 5 2 3 4 6 9 8 5 2 3 4 5 9 8 6 2 3 4 5 6 8 9 i i j i j i i j i j j j 11/12/2018 IT 179

Logarithm (binary search) Linear (sequential search) n log n (quick sort) Quadratic (n2, selection sort, insertion sort) Polynomial (Primality test) Nondeterministic polynomial (Traveling Salesman problem) Exponential (en Backtracking) 11/12/2018 IT 179

Big-Oh (): Linear Search TlinearSearch(n) = 3n + 3 = (n) for c = 4 and n0 = 0, in the worst case. n2 TlinearSearch(n) = (n2) But this is an abuse of O-notation 4n 3n + 3 11/12/2018 IT 179

Some Common Computing Times log2n n n log2n n2 2n 1 2 4 8 16 3 24 64 256 65,536 5 32 160 1,024 4,294,967,296 6 384 4,096 1.84  1019 7 128 896 16,384 3.40  1038 2,048 1.16  1077 9 512 4,608 262,144 1.34  10154 10 10,240 1,048,576 1.80  10308 11/12/2018 IT 179