1 / 22

What/how do we care about a program?

Learn about the importance of robustness and error handling in Java programming, covering topics such as software testing, efficiency, algorithm analysis, and how to effectively deal with exceptions. Explore various types of errors, exception throwing, try-catch-finally blocks, and the Java Exception Hierarchy. Understand the significance of handling both checked and unchecked exceptions to ensure program correctness. Discover best practices for handling exceptions, testing methodologies, and analyzing algorithm efficiency for optimal performance.

bakerb
Download Presentation

What/how do we care about a program?

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. 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 IT 179

  3. Exceptions In Java, they are classes • ThrowingExceptions 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) IT 179

  4. 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 IT 179

  5. Try-Catch-Finally Blocks try { 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 or any of its subclasses } … . . . other catch clauses … catch ( ExceptionTypeN exception ) { program statements to handle exceptions of type ExceptionTypeN or any of its subclasses } finally { this block is optional; this block will execute whether or not an exception is thrown }

  6. catch (NumberFormatException ex) { System.out.println("\n"+ex.toString()); ex.printStackTrace(); } catch (IllegalArgumentException ex) { System.out.println("\n"+ex.toString()); ex.printStackTrace(); } catch (ArrayIndexOutOfBoundsException ex) { System.out.println("\n"+ex.toString()); ex.printStackTrace(); } catch (IndexOutOfBoundsException ex) { System.out.println("\n"+ex.toString()); ex.printStackTrace(); } catch (RuntimeException ex) { System.out.println("\n"+ex.toString()); ex.printStackTrace(); } finally { System.out.println("\nWe are done. Finally!!"); } 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"); System.out.println("\n"+ex.toString()); ex.printStackTrace(); } IT 179

  7. 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); } IT 179

  8. 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. IT 179

  9. 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: IT 179

  10. Java’s Exception Hierarchy Unchecked Checked

  11. 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 IT 179

  12. 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). IT 179

  13. Time Complexity: Count Instructions How many times will each instruction execute? IT 179

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

  15. Consider the worst part of the program if (....) { ......... ......... } else { ......... ......... } n2  n3 n3 IT 179

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

  17. Linear Search IT 179

  18. // select the minimum between a[s] to the end // and return the index of the minimum // privateint 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] // privatevoid 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) IT 179

  19. publicvoid sort(int a[]) { // select right element for a[i] for (int i = 0; i<a.length-1; i++) { intj = min(a,i); exchange(a,i,j); } } Selection Sort (a quadratic sort) O(n2) i i j i j i i j i j j j IT 179

  20. 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) IT 179

  21. 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 IT 179

  22. Some Common Computing Times IT 179

More Related