1 / 18

Exception Handling

Exception Handling. Kapitel 9. Agenda. Exceptions try, throw and catch Skapa en egen exception-klass Multipla throw / catch Slänga vidare en exception Olika sorters exceptions För- och eftervillkor. try, throw and catch. Scanner kbd = new Scanner(System.in); try { int a = 5;

gen
Download Presentation

Exception Handling

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. Exception Handling Kapitel 9

  2. Agenda • Exceptions • try, throw and catch • Skapa en egen exception-klass • Multipla throw / catch • Slänga vidare en exception • Olika sorters exceptions • För- och eftervillkor

  3. try, throw and catch Scanner kbd = new Scanner(System.in); try { int a = 5; int b = kbd.nextInt(); if(b == 0) throw new Exception("Kan inte dela med noll!"); else System.out.println("a / b = " + a/b); } catch(Exception e) { System.out.println(e.getMessage()); }

  4. try, throw and catch • Try-blocket • Innehåller kod där nått kan gå snett • Om det går snett så “kastar” (throw) vi en exception • Catch-blocket • Fångar “kastet” • Nästan som en metod

  5. Fördefinierade exception-klasser • FileNotFoundException • IOException • ClassNotFoundException • NoSuchMethodException

  6. Skapa en egen exception-klass public class delamednoll extends Exception { public delamednoll(String message) { super(message); } }

  7. Skapa en egen exception-klass Scanner kbd = new Scanner(System.in); try { int a = 5; int b = kbd.nextInt(); if(b == 0) throw new delamednoll("Kan inte dela med noll!!!"); else System.out.println("a / b = " + a/b); } catch(delamednoll e) { System.out.println(e.getMessage()); }

  8. Multipla throw/catch try { int b = kbd.nextInt(); if(b == 0) throw new delamednoll("Hej 1"); else if(b < 0) throw new negativtal("Hej 2"); } catch(delamednoll e) { System.out.println(e.getMessage()); } catch(negativtal e) { System.out.println(e.getMessage()); }

  9. Slänga vidare en exception public static void main(String[ ] args) { Scanner kbd = new Scanner(System.in); try { int a = 5, b = kbd.nextInt(); int c = dela(a,b); System.out.println(c); } catch(delamednoll e) { System.out.println(e.getMessage()); } }

  10. Slänga vidare en exception public static int dela(int a, int b) throws delamednoll { if(b == 0) throw new delamednoll("Inte bra!"); return a/b; }

  11. Olika sorters exceptions • Checked exception • Måste tas omhand i ett catch-block (eller throws) • Unchecked exception • Behöver inte tas omhand i ett catch-block • Array index out of bounds • Division med noll • Avslutar programmet

  12. Olika sorters exceptions

  13. Errors • Genereras vid onormala tillstånd • Vad nu det betyder  • Out of memory • Behöver inte tas omhand • Som unchecked

  14. För- och eftervillkor • Förvillkor (pre) • Vad som ska gälla innan metoden anropas • Om det inte är uppfyllt är resultatet odefinierat • Eftervillkor (post) • Vad som ska gälla när metoden är klar • Under förutsättning att förvillkoret var uppfyllt

  15. Metod för division //pre: nämnare != 0 //post: kvoten mellan taljare och //namnare har returnerats double dividera(double taljare, double namnare) { return taljare/namnare; }

  16. Kontraktsprogrammering • taUtPengar • pre: ut <= saldo • post: saldo har minskats med ”ut” kronor • sattInPengar • pre: in >= 0 • saldo har ökats med ”in” kronor

  17. Summering • Exceptions • Är ett objekt • Separerar normalfallet från ovanliga fall • try, throw and catch • Skapa en egen exception-klass • Multipla throw / catch • Slänga vidare en exception • throws

  18. Summering • Olika sorters exceptions • Checked • Unchecked • Error • Kontraktsprogrammering • pre / post

More Related