220 likes | 238 Views
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.
E N D
What/how do we care about a program? Robustness Error Handling Correctness Software Testing Efficiency (speed, space) Algorithm Analysis 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 IT 179
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
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
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 }
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
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
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
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
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 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). IT 179
Time Complexity: Count Instructions How many times will each instruction execute? IT 179
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
Consider the worst part of the program if (....) { ......... ......... } else { ......... ......... } n2 n3 n3 IT 179
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
Linear Search IT 179
// 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
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
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
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
Some Common Computing Times IT 179