logging in or signing up 10Exceptions lyon Download Post to : URL : Related Presentations : Share Add to Flag Embed Email Send to Blogs and Networks Add to Channel Uploaded from authorPOINT lite Insert YouTube videos in PowerPont slides with aS Desktop Copy embed code: (To copy code, click on the text box) Embed: URL: Thumbnail: WordPress Embed Customize Embed The presentation is successfully added In Your Favorites. Views: 6 Category: Education License: All Rights Reserved Like it (0) Dislike it (0) Added: September 19, 2010 This Presentation is Public Favorites: 0 Presentation Description No description available. Comments Posting comment... Premium member Presentation Transcript 10. Exceptions : 10. Exceptions Checked vs. Unchecked Arian 5 : Arian 5 June 4, 1996 : June 4, 1996 The inertial guidance computer had told the steering computer that the rocket had gone way off course, when in fact it was not off course at all. They borrowed the computer from A4! : They borrowed the computer from A4! guidance computer converted a 64-bit floating point number into a 16-bit signed integer number. After 36 seconds N was larger than 32768, which is the largest possible 16-bit signed integer! steering computer, was fed bad numbers! Java to the rescue : Java to the rescue T- 39 seconds the rocket exploded, destroying the rocket and cargo valued at half a billion dollars They needed to throw an arithmetic number EXCEPTION…. Exception Class : Exception Class Resides in Java.lang Subclasses of Exception are checked exceptions Try-catch statements are required! Defer error handling until later. try { doTheGoodThing()}catch(Exception e){} Why not guard the input? : Why not guard the input? public double invert f(double x) { if (x==0) System.out.println(“danger x is zero!”); return 0; return 1/x; } Create our own Exception : Create our own Exception class CreditCardException extends Exception { public static check (int i) throws CreditCardException { if (i <= 0) throw new CreditCardException(); } } Checking the Card : Checking the Card public void checkCard(int ccn) { try { CreditCardException.check(ccn); } catch(CreditCardException e) { e.printStackTrace(); } } Rule -try-catch : Rule -try-catch You must throw a checked exception OR try to catch it! public class Wow { public void printCheck(int ccn) throws CreditCardException { System.out.println(“checking ccn:”+ccn); CreditCardException.check(ccn); }} Catching Multiple Exceptions : Catching Multiple Exceptions try { ..... } catch (Exception1 e) {// the subclass ..... } catch (Exception2 e) {// parent class .... } Catch Rule : Catch Rule Have as many catches as you like, but you must at least one! You can always catch all exception with: try { } catch (Exception e) { e.printStackTrace(); Log.store(e); } Example Exception : Example Exception class IOException extends Exception{ } class FileIOException extends IOException { } class UserIOException extends IOException { } Finally! : Finally! Finally is optional in the try-catch block. try { ..... } catch (Exception e) { } finally { // this is optional } Finally Rule1 : Finally Rule1 Finally is ALWAYS executed unless the JVM fails for some out-side cause. Finally is good for house keeping close a file close database connections, etc. IsDigits : IsDigits public static int getNumberOfDigits(String s) { char ca[] = s.toCharArray(); int nod = 0; for (int i=0; i < ca.length; i++) { char c = ca[i]; if (Character.isDigit(c)) nod++; } return nod; } Make a cce : Make a cce public class CreditCardException extends Exception { } CheckCC : CheckCC public static void checkCreditCard(String cc) throws CreditCardException { if (getNumberOfDigits(cc) != 16) throw new CreditCardException(); } Make app : Make app public static void main(String args[]) { try { IsDigitExample.checkCreditCard( "123467wefewfewewrwe890123456"); System.out.println("MO MONEY"); }catch(CreditCardException cce) { System.out.println("DEADBEAT!"); } } You do not have the permission to view this presentation. In order to view it, please contact the author of the presentation.
10Exceptions lyon Download Post to : URL : Related Presentations : Share Add to Flag Embed Email Send to Blogs and Networks Add to Channel Uploaded from authorPOINT lite Insert YouTube videos in PowerPont slides with aS Desktop Copy embed code: (To copy code, click on the text box) Embed: URL: Thumbnail: WordPress Embed Customize Embed The presentation is successfully added In Your Favorites. Views: 6 Category: Education License: All Rights Reserved Like it (0) Dislike it (0) Added: September 19, 2010 This Presentation is Public Favorites: 0 Presentation Description No description available. Comments Posting comment... Premium member Presentation Transcript 10. Exceptions : 10. Exceptions Checked vs. Unchecked Arian 5 : Arian 5 June 4, 1996 : June 4, 1996 The inertial guidance computer had told the steering computer that the rocket had gone way off course, when in fact it was not off course at all. They borrowed the computer from A4! : They borrowed the computer from A4! guidance computer converted a 64-bit floating point number into a 16-bit signed integer number. After 36 seconds N was larger than 32768, which is the largest possible 16-bit signed integer! steering computer, was fed bad numbers! Java to the rescue : Java to the rescue T- 39 seconds the rocket exploded, destroying the rocket and cargo valued at half a billion dollars They needed to throw an arithmetic number EXCEPTION…. Exception Class : Exception Class Resides in Java.lang Subclasses of Exception are checked exceptions Try-catch statements are required! Defer error handling until later. try { doTheGoodThing()}catch(Exception e){} Why not guard the input? : Why not guard the input? public double invert f(double x) { if (x==0) System.out.println(“danger x is zero!”); return 0; return 1/x; } Create our own Exception : Create our own Exception class CreditCardException extends Exception { public static check (int i) throws CreditCardException { if (i <= 0) throw new CreditCardException(); } } Checking the Card : Checking the Card public void checkCard(int ccn) { try { CreditCardException.check(ccn); } catch(CreditCardException e) { e.printStackTrace(); } } Rule -try-catch : Rule -try-catch You must throw a checked exception OR try to catch it! public class Wow { public void printCheck(int ccn) throws CreditCardException { System.out.println(“checking ccn:”+ccn); CreditCardException.check(ccn); }} Catching Multiple Exceptions : Catching Multiple Exceptions try { ..... } catch (Exception1 e) {// the subclass ..... } catch (Exception2 e) {// parent class .... } Catch Rule : Catch Rule Have as many catches as you like, but you must at least one! You can always catch all exception with: try { } catch (Exception e) { e.printStackTrace(); Log.store(e); } Example Exception : Example Exception class IOException extends Exception{ } class FileIOException extends IOException { } class UserIOException extends IOException { } Finally! : Finally! Finally is optional in the try-catch block. try { ..... } catch (Exception e) { } finally { // this is optional } Finally Rule1 : Finally Rule1 Finally is ALWAYS executed unless the JVM fails for some out-side cause. Finally is good for house keeping close a file close database connections, etc. IsDigits : IsDigits public static int getNumberOfDigits(String s) { char ca[] = s.toCharArray(); int nod = 0; for (int i=0; i < ca.length; i++) { char c = ca[i]; if (Character.isDigit(c)) nod++; } return nod; } Make a cce : Make a cce public class CreditCardException extends Exception { } CheckCC : CheckCC public static void checkCreditCard(String cc) throws CreditCardException { if (getNumberOfDigits(cc) != 16) throw new CreditCardException(); } Make app : Make app public static void main(String args[]) { try { IsDigitExample.checkCreditCard( "123467wefewfewewrwe890123456"); System.out.println("MO MONEY"); }catch(CreditCardException cce) { System.out.println("DEADBEAT!"); } }