DOC PREVIEW
USF CS 112 - Exceptions and ArrayLists

This preview shows page 1-2-21-22 out of 22 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 22 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 22 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 22 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 22 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 22 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

{small lecturenumber - heblocknumber :} Errorsaddtocounter {blocknumber}{1}{small lecturenumber - heblocknumber :} Error Checkingaddtocounter {blocknumber}{1}{small lecturenumber - heblocknumber :} Error Checkingaddtocounter {blocknumber}{1}{small lecturenumber - heblocknumber :} Exceptionsaddtocounter {blocknumber}{1}{small lecturenumber - heblocknumber :} Exceptionsaddtocounter {blocknumber}{1}{small lecturenumber - heblocknumber :} Try-Catch Blockaddtocounter {blocknumber}{1}{small lecturenumber - heblocknumber :} Exceptionsaddtocounter {blocknumber}{1}{small lecturenumber - heblocknumber :} Exceptionsaddtocounter {blocknumber}{1}{small lecturenumber - heblocknumber :} Exceptionsaddtocounter {blocknumber}{1}{small lecturenumber - heblocknumber :} Exceptionsaddtocounter {blocknumber}{1}{small lecturenumber - heblocknumber :} Exceptionsaddtocounter {blocknumber}{1}{small lecturenumber - heblocknumber :} Exceptionsaddtocounter {blocknumber}{1}{small lecturenumber - heblocknumber :} Uncaught Exceptionsaddtocounter {blocknumber}{1}{small lecturenumber - heblocknumber :} Uncaught Exceptionsaddtocounter {blocknumber}{1}{small lecturenumber - heblocknumber :} Uncaught Exceptionsaddtocounter {blocknumber}{1}{small lecturenumber - heblocknumber :} Uncaught Exceptionsaddtocounter {blocknumber}{1}{small lecturenumber - heblocknumber :} Different Kinds of Exceptionsaddtocounter {blocknumber}{1}{small lecturenumber - heblocknumber :} Errors Aheadaddtocounter {blocknumber}{1}{small lecturenumber - heblocknumber :} ArrayList Funaddtocounter {blocknumber}{1}{small lecturenumber - heblocknumber :} ArrayList Funaddtocounter {blocknumber}{1}{small lecturenumber - heblocknumber :} In-Class Assignmentaddtocounter {blocknumber}{1}Intro to Computer Science IICS112-2012S-06Exceptions and ArrayListsDavid GallesDepartment of Computer ScienceUniversity of San Francisco06-0: ErrorsErrors can occur in programInvalid input / bad dataUnexpected situationLogic error in codeLike to handle these errors gracefully, not just haltthe programRunning a web server, don’t want one piece ofbad data to bring the whole thing down06-1: Error CheckingWe could check for any concievable errorArrayList<Integer> A;// Initialize AA.set(i, new Integer(x/y));06-2: Error CheckingWe could check for any concievable errorif (i >= 0 && i < A.size()){if (y != 0){A.set(i, new Integer(x / y);}else{// Handle division by zero case}// Handle outside bounds of the array case}Problems with this method?06-3: ExceptionsWe can let the system catch all the errors for usA.set(i, new Integer(x / y));Throws an exception if i < 0, i >= A.size(), y == 0.Program ends.Problems with this method?06-4: ExceptionsWe can let the system catch the errors for usWe can “catch” the errors ourselvestry{A.set(i, new Integer(x,y));}catch (Exception e){// do some work to clean up after the exception}06-5: Try-Catch Blocktry{// Any Java Code}catch (Exception e){// Any Java Code}If an exception is rasied inside the try block:Stop immediately and execute the code in thecatch blockConinue after the catch block as normalIf no exception is raised inside the try blockIgnore the code in the catch block06-6: Exceptionsint x;int y;try{x = 3;y = 0;x = x / y;System.out.println("Can’t get here!");}catch (Exception e){System.out.println("Exception caught!");}System.out.println("Done with try block!");06-7: Exceptionsint x;int y;try{x = 3;y = 5;x = x / y;System.out.println("We will get here!");}catch (Exception e){System.out.println("We won’t get here!");}System.out.println("Done with try block!");06-8: ExceptionsArrayList<Integer> A = new ArrayList<Integer>();for (int i = 0; i < 10; i++){A.add(new Integer(i));}try{for (int i = 0; i < 10; i++)System.out.println(i);for (int i = 10; i > 0; i--)System.out.println(i);}catch (Exception e){System.out.println("Error!");}System.out.println("Done with try block!");06-9: ExceptionsArrayList<Integer> A = new ArrayList<Integer>();for (int i = 0; i < 10; i++){A.add(new Integer(i));}try{for (int i = 0; i < 10; i++)System.out.println(A.get(i));for (int i = 10; i > 0; i--)System.out.println(A.get(i));}catch (Exception e){System.out.println("Error!");}System.out.println("Done with try block!");06-10: ExceptionsVariables declared within a try block are not visibleoutsideActually, variables declated within any block arenot visible outside the blocktry{ArrayList<Integer> A = new ArrayList<Integer>();for (int i = 0; i < 10; i++)A.add(new Integer(i));}catch (Exception e){System.out.println("Error!");}A.set(3, new Integer(5)); // ERROR!06-11: ExceptionsWhat went wrong?try{A.set(new Integer(x/y));}catch (Exception e){System.out.println(e.getMessage());}We’ll do more with this after Inheritance06-12: Uncaught Exceptionsint divide(int x, int y){int result = x / y;System.out.println(result);return result;}void foo(){int x = 6;x = divide(x, 2);x = divide(x, 1);x = divide(x, 0);x = divide(x, 3);}06-13: Uncaught Exceptionsint divide(int x, int y){int result = x / y;System.out.println(result);return result;}void foo(){try{int x = 6;x = divide(x, 3);x = divide(x, 2);x = divide(x, 0);x = divide(x, 2);}catch (ArithmeticException e){System.out.println("Error!");}}06-14: Uncaught Exceptionsint divide(int x, int y) void foo(){ {int result = x / y; ArrayList<int> A = new ArrayList<int>();System.out.println(result); A.add(new Integer(4));return result; A.add(new Integer(0));} A.add(new Integer(3));void divideBySelf(ArrayList<Integer> A) try{ {for (int i = 0; i < A.size(); i++ divideBySelf(A);{ }int res = divide(A.get(i), A.get(i));A.set(res); catch (ArithmeticException e)} {} System.out.println("Excp. caught!");}}06-15: Uncaught Exceptionsclass Silly {public int x;int badFunc() void bar(){ {x++; x++;int y = x / 0; foo();x++; x++;} }void foo() void start(){ {x++; trybadFunc(); {x++; x = 0;} bar();}catch (Exception e) { }System.out.println(x);}06-16: Different Kinds of ExceptionsThere are many different kinds of exceptionsArray Out of boundsArithmetic (divide by zero)I/O (file doesn’t exist, etc)We’ll come back to different kinds of exceptionsafter we’ve covered inheritance06-17: Errors AheadYou can warn other programmers using you codethat your code might throw an exceptionThey will need to either deal with the exceptionthemselves, or throw it on to who called themWe’ve already seen this (class File), examples inclass(Examples)06-18: ArrayList FunWrite a function that reverse an ArrayList of stringsvoid reverse(ArrayList<string> list){}06-19:


View Full Document

USF CS 112 - Exceptions and ArrayLists

Documents in this Course
Structs

Structs

4 pages

Trees

Trees

25 pages

Strings

Strings

27 pages

Queues

Queues

3 pages

Trees

Trees

24 pages

Arrays

Arrays

5 pages

ArrayList

ArrayList

24 pages

Stacks

Stacks

2 pages

Stacks

Stacks

8 pages

Trees

Trees

24 pages

Stacks

Stacks

8 pages

Queues

Queues

16 pages

Queues

Queues

17 pages

Queues

Queues

17 pages

Load more
Download Exceptions and ArrayLists
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Exceptions and ArrayLists and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Exceptions and ArrayLists 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?