java programming unit 7
play

Java Programming Unit 7 Error Handling. Excep8ons. - PowerPoint PPT Presentation

Java Programming Unit 7 Error Handling. Excep8ons. (c) Yakov Fain 2014 Run8me errors An excep8on is an run-8me error that may stop


  1. Java ¡Programming ¡ ¡ Unit ¡7 ¡ Error ¡Handling. ¡Excep8ons. ¡ ¡ (c) ¡Yakov ¡Fain ¡ ¡2014 ¡

  2. Run8me ¡errors ¡ An ¡excep8on ¡is ¡an ¡run-­‑8me ¡error ¡that ¡may ¡stop ¡the ¡ execu8on ¡of ¡your ¡program. ¡For ¡example: ¡ ¡ -­‑ someone ¡deleted ¡a ¡file ¡that ¡a ¡program ¡usually ¡reads ¡ -­‑ The ¡client ¡calls ¡the ¡server, ¡which ¡doesn’t ¡respond ¡ -­‑ A ¡program ¡variable ¡should ¡point ¡at ¡the ¡instance ¡of ¡an ¡ ¡ object, ¡but ¡it ¡was ¡never ¡instan8ated. ¡ ¡ Java ¡compiler ¡oRen ¡forces ¡you ¡to ¡add ¡add ¡ excep%on ¡ handling ¡in ¡your ¡programs. ¡ (c) ¡Yakov ¡Fain ¡ ¡2014 ¡

  3. Stack ¡Trace ¡ Run ¡this ¡program ¡and ¡you’ll ¡see ¡a ¡ stack ¡ 1 class TestStackTrace{ � trace ¡output ¡in ¡the ¡system ¡console. ¡ ¡ 2 TestStackTrace() � 3 { � ¡ 4 divideByZero(); � ¡ 5 } � java ¡TestStackTrace ¡ 6 � 7 int divideByZero() � ¡ ¡ 8 { � ¡ ¡ ¡ ¡ ¡Excep8on ¡in ¡thread ¡"main" ¡ ¡ ¡ 9 // Purposely dividing by zero � ¡ ¡ ¡ ¡ ¡java.lang.Arithme8cExcep8on: ¡/ ¡by ¡zero ¡ 9 return 25/0; � 10 } � ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡at ¡ 11 public � TestStackTrace.divideByZero(TestStackTrace.java:9) ¡ 12 static void main(String[]args) � ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡at ¡TestStackTrace.<init>(TestStackTrace.java:4) ¡ 13 { � ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡at ¡TestStackTrace.main(TestStackTrace.java:14) ¡ 14 new TestStackTrace(); � 15 � ¡ 16 } � ¡ 17 } � Read ¡it ¡from ¡the ¡bo\om ¡up! ¡ ¡ ¡ ¡ (c) ¡Yakov ¡Fain ¡ ¡2014 ¡

  4. Walkthrough ¡1 ¡ • Download ¡the ¡source ¡code ¡from ¡the ¡Lesson ¡13 ¡ and ¡import ¡it ¡into ¡Eclipse ¡ ¡ • Run ¡the ¡TestStackTrace ¡program ¡and ¡observe ¡ the ¡stack ¡trace ¡in ¡the ¡Console ¡view ¡ • Examine ¡the ¡code ¡of ¡the ¡TestStackTrace2, ¡which ¡ handles ¡this ¡excep8on. ¡Run ¡this ¡program ¡ (c) ¡Yakov ¡Fain ¡ ¡2014 ¡

  5. Try-­‑Catch ¡Blocks ¡ If ¡a ¡piece ¡of ¡code ¡may ¡result ¡in ¡a ¡ run-­‑%me ¡ error, ¡Java ¡ can ¡force ¡you ¡ to ¡enclose ¡it ¡in ¡the ¡ try-catch block. ¡ ¡ ¡For ¡example, ¡ ¡reading ¡a ¡file ¡may ¡generate ¡an ¡ IOException : ¡ ¡ try{ � fileCustomer.read(); // the file may be corrupted or missing � } � catch (IOException e){ � System.out.println("There seems to be a problem with the file customers."); � } � ¡ Checked ¡excep%ons ¡ are ¡the ¡ones ¡that ¡you ¡can ¡foresee, ¡but ¡can’t ¡fix ¡ ¡ inside ¡the ¡program. ¡For ¡example ¡if ¡the ¡file ¡with ¡the ¡customer ¡data ¡ ¡ ¡ is ¡corrupted, ¡there ¡is ¡nothing ¡your ¡program ¡can ¡do ¡about ¡it. ¡ ¡ (c) ¡Yakov ¡Fain ¡ ¡2014 ¡

  6. Excep8ons ¡Hierarchy ¡ Throwable A ¡superclass ¡of ¡all ¡excep8ons. ¡ � Error Exception � RuntimeException LoveFailedException IOException � � � RuntimeException � Subclasses ¡of ¡the ¡class ¡ Error ¡and ¡ RuntimeException ¡ ¡are ¡fatal ¡errors ¡ ¡ and ¡are ¡called ¡ unchecked ¡excep%ons , ¡and ¡are ¡not ¡required ¡to ¡be ¡ ¡caught. ¡ Subclasses ¡of ¡ Exception ¡(excluding ¡ RuntimeException ) ¡are ¡called ¡ ¡ checked ¡excep8ons ¡and ¡have ¡to ¡be ¡handled ¡in ¡your ¡code. ¡ ¡ ¡ (c) ¡Yakov ¡Fain ¡ ¡2014 ¡

  7. Catching ¡Mul8ple ¡Excep8ons ¡Before ¡Java ¡7 ¡ public void getCustomers(){ � try{ � fileCustomers.read(); // may throw an error � � } catch(FileNotFoundException e){ � System.out.println(“Can not find file Customers”); � }catch(EOFException e1){ � System.out.println(“Done with file read”); � }catch(IOException e2){ � System.out.println(“Problem reading file “ + � e2.getMessage()); � } catch (Exception e3){ � System.out.println(“Something went wrong”); � } � } � � (c) ¡Yakov ¡Fain ¡ ¡2014 ¡

  8. Catching ¡Mul8ple ¡Excep8ons ¡in ¡Java ¡7 ¡ Java ¡7 ¡introduced ¡catching ¡mul8ple ¡excep8ons ¡in ¡one ¡ catch ¡block: ¡ ¡ ¡ � public void getCustomers(){ � try{ � fileCustomers.read(); // may throw an error � � } catch(FileNotFoundException | EOFException | IOException e){ � System.out.println(“Problem reading file “ +e.getMessage()); � } catch (Exception e1){ � System.out.println(“Something else went wrong”); � } � } � (c) ¡Yakov ¡Fain ¡ ¡2014 ¡

  9. The ¡ throws ¡Keyword ¡ class ¡CustomerList{ ¡ ¡ Propaga:on ¡of ¡Excep:ons ¡ ¡ ¡ ¡ ¡void ¡getAllCustomers() ¡throws ¡IOExcep:on { ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡// ¡Some ¡other ¡code ¡goes ¡here ¡ ¡ If ¡you ¡are ¡not ¡planning ¡to ¡ ¡ ¡ ¡ ¡ ¡// ¡Don’t ¡use ¡try/catch ¡if ¡you ¡are ¡not ¡handling ¡excep8ons ¡ ¡ ¡ ¡ ¡ ¡ ¡// ¡ ¡in ¡this ¡method ¡ handle ¡excep8ons ¡in ¡a ¡ ¡ ¡ ¡ ¡ ¡file.read(); ¡ ¡ ¡ ¡ ¡} ¡ method, ¡add ¡a ¡ throws ¡in ¡the ¡ ¡ ¡ method ¡declara8on. ¡ ¡ ¡public ¡sta8c ¡void ¡main(String[] ¡args){ ¡ ¡ ¡ ¡ ¡ ¡ ¡System.out.println(“Customer ¡List”); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡// ¡Some ¡other ¡code ¡goes ¡here ¡ ¡ ¡ ¡ ¡ Handling ¡Excep:ons ¡ ¡ ¡ ¡ ¡ ¡try{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡// ¡Since ¡getAllCustomers() ¡declares ¡an ¡excep8on, ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡// ¡either ¡ ¡handle ¡ ¡it ¡over ¡here, ¡or ¡re-­‑throw ¡it ¡ ¡ ¡ ¡ Now ¡the ¡caller ¡of ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ getAllCustomers(); ¡ ¡ ¡ ¡ ¡ ¡ ¡} ¡ getAllCustomers() will ¡ ¡ ¡ ¡ ¡ ¡catch(IOExcep8on ¡e){ ¡ have ¡to ¡handle ¡the ¡excep8on. ¡ ¡ ¡ ¡ ¡ ¡ ¡// ¡Excep8on ¡is ¡considered ¡handled ¡ ¡ ¡ ¡ ¡ ¡ ¡System.out.println(“Customer ¡List ¡is ¡not ¡available”); ¡ ¡ ¡ ¡ ¡ ¡} ¡ } ¡ (c) ¡Yakov ¡Fain ¡ ¡2014 ¡

  10. Walkthrough ¡2 ¡ 1. Visit ¡the ¡help ¡page ¡for ¡the ¡class ¡ FileInputStream . ¡ ¡ ¡ h\p://download.oracle.com/javase/7/docs/api/java/io/FileInputStream.html ¡ ¡ ¡ ¡ 2. ¡Browse ¡the ¡help ¡info ¡on ¡various ¡methods ¡of ¡this ¡class ¡and ¡ ¡ ¡ ¡ ¡ ¡pay ¡a\en8on ¡to ¡their ¡ throws ¡statements. ¡ ¡ 3. ¡Visit ¡the ¡help ¡page ¡for ¡the ¡class ¡ IOException and 
 look ¡at ¡its ¡direct ¡known ¡subclasses: ¡ ¡ ¡ h\p://download.oracle.com/javase/7/docs/api/java/io/IOExcep8on.html ¡ ¡ ¡ Take ¡a ¡look ¡at ¡its ¡direct ¡known ¡subclasses. ¡ ¡ (c) ¡Yakov ¡Fain ¡ ¡2014 ¡

  11. The ¡ finally ¡keyword ¡ If ¡there ¡is ¡a ¡piece ¡of ¡code ¡that ¡ ¡ try{ � file.read(); � must ¡be ¡executed ¡ regardless ¡ ¡ // file.close(); don’t close files inside try � } � of ¡the ¡success ¡or ¡failure ¡of ¡ ¡ catch(Exception e){ � the ¡code ¡inside ¡ try{} , ¡ e.printStackTrace(); � } � ¡put ¡it ¡under ¡the ¡ ¡ finally { � if (file != null){ � clause ¡f inally . ¡ ¡ try{ � file.close(); � }catch(IOException e1){ � e1.printStackTrace(); � } � } � � Prior ¡to ¡Java ¡7, ¡finally ¡was ¡the ¡only ¡right ¡place ¡to ¡ ¡external ¡resources ¡ like ¡database ¡connec8ons ¡or ¡open ¡files. ¡ (c) ¡Yakov ¡Fain ¡ ¡2014 ¡

  12. Java ¡7: ¡ ¡try ¡statement ¡with ¡resources ¡ try-­‑with-­‑resources ¡that ¡supports ¡ automa%c ¡closing ¡ of ¡the ¡resources ¡ without ¡the ¡need ¡to ¡use ¡the ¡ finally ¡clause: ¡ InputStream myFileInputStream = null; � � try (myFileInputStream=new FileInputStream(“customers.txt”);) { 
 // the code that reads data from customers.txt goes here � } catch (Exception e){ � e.printStackTrace(); � } � This ¡works ¡only ¡if ¡the ¡resource ¡implements ¡java.lang.AutoCloseable ¡or ¡ ¡java.io.Closeable ¡interface, ¡which ¡declares ¡just ¡one ¡method ¡close(). ¡ ¡ See ¡the ¡example ¡of ¡ try ¡with ¡automa8c ¡resource ¡management ¡is ¡here: ¡ h\p://java.dzone.com/ar8cles/java-­‑7-­‑new-­‑try-­‑resources ¡ (c) ¡Yakov ¡Fain ¡ ¡2014 ¡

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend