j
play

J Introduction IS311 Programming Concepts Users have high - PowerPoint PPT Presentation

1 2 J Introduction IS311 Programming Concepts Users have high expectations for the code we produce. Users will use our programs in AVA unexpected ways. Due to design errors or coding errors, Exception Handling


  1. � 1 � 2 J Introduction IS311 Programming Concepts • Users have high expectations for the code we produce. • Users will use our programs in AVA unexpected ways. • Due to design errors or coding errors, Exception Handling 
 การจาดการสิ้งผีดปรกตี our programs may fail in unexpected ways during execution � 3 � 4 Introduction Errors and Error Handling • It is our responsibility to produce • An Error is any unexpected result obtained from a program during execution. quality code that does not fail • Unhandled errors may manifest themselves unexpectedly. as incorrect results or behavior, or as • Consequently, we must design error abnormal program termination. handling into our programs. • Errors should be handled by the programmer, to prevent them from reaching the user.

  2. � 5 � 6 Errors and Error Handling Errors and Error Handling • Some typical causes of errors: • More typical causes of errors: – Memory errors (i.e. memory incorrectly – Array errors (i.e. accessing element –1) allocated, memory leaks, “null pointer”) – Conversion errors (i.e. convert ‘q’ to a – File system errors (i.e. disk is full, disk has number) been removed) – Can you think of some others? – Network errors (i.e. network is down, URL does not exist) – Calculation errors (i.e. divide by 0) � 7 � 8 Errors and Error Handling Errors and Error Handling • Traditional Error Handling • Exceptions – a better error handling – Exceptions are a mechanism that provides the – 1. Every method returns a value (flag) indicating best of both worlds. either success, failure, or some error condition. The calling method checks the return flag and – Exceptions act similar to method return flags in takes appropriate action. that any method may raise and exception should it encounter an error. – Downside: programmer must remember to always check the return value and take – Exceptions act like global error methods in that appropriate action. This requires much code the exception mechanism is built into Java; (methods are harder to read) and something may exceptions are handled at many levels in a get overlooked. program, locally and/or globally.

  3. � 9 � 10 Exceptions and JVM Exceptions Hierarchy Throwable • When an exception takes place, the Java Virtual Machine (JVM) creates an exception object to identify the type of Error Exception exemption that occurred • The Throwable class is the super class *** of all error and exception types *** 
 VirtualMachineError RuntimeException (nonruntime Exceptions) generated by the JVM or Java programs • Three Throwable subclass categories *** are possible: Error, Runtime and ArithmeticException NullPointerException ClassCastException Nonruntime Exceptions *** หมายถืงยางมึอึก แต้ไม้กล้าวถืง 
 คลาสที้ระบายสึแดงหมายถืงมึข๊อผีดพลาดร๊ายแรง โปรแกรมทิอะไรต้อไม้ได๊ � 11 � 12 The try catch finally statement 2 type of Exceptions try { • Checked: are the exceptions that are checked at compile time. Checked exceptions // คำสั่งต่างๆ ที่ต้องการทำและอาจก่อให้เกิดสิ่งผิดปรกติ must be handled or declared otherwise it } catch ( ExceptionType1 e) { causes compile time error. // คำสั่งที่ใช้จัดการเมื่อเกิดสิ่งผิดปรกติชนิด ExceptionType1 • Unchecked : are the exceptions that are not checked at compiled time. No need to } catch ( ExceptionType2 e) { handled or declared unchecked exceptions, // คำสั่งที่ใช้จัดการเมื่อเกิดสิ่งผิดปรกติชนิด ExceptionType2 they wont’t cause any compile time error. 
 throw e; // โยนสิ่งผิดปรกติไปให้อ๊อบเจกต์ e จัดการต่ออีกทอด คลาสที้อยู้ภายใต๊คลาส RuntimeException } finally { เป่นคลาสแบบ unchecked ทั๊งสิ๊น เราจะเรึยน // คำสั่งที่ต้องทำเสมอ ไม่ว่าจะเกิดสิ่งผิดปรกติชนิดใดขึ้นหรือไม่ก็ตาม การจาดการ exception สิหราบคลาสในกลุ้มนี๊ }

  4. � 13 � 14 Exception-Handling Mechanism Keywords for Java Exceptions • throws ( ใช๊ตอนประกาศเมท่อด ) 
 1. Mechanism for creating special exception classes (whose instances are called exception objects ) Describes the exceptions which can be raised by a method. • throw 
 2. The statement throw e is used to signal the Raises an exception to the first available handler in the call occurrence of an exception and return control to stack, unwinding the stack along the way. the calling method and e refers to an exception • try 
 object Marks the start of a block associated with a set of exception 3. The statement try/catch allows the calling handlers. method to “ catch ” the “ thrown ” exception object • catch 
 and take appropriate actions If the block enclosed by the try generates an exception of this type, control moves here. • finally 
 Always called when the try block concludes, and after any necessary catch handler is complete. � 15 � 16 Error & Exception Control Flow and Exceptions • When exception is thrown control returns • Error Class through the methods called in reverse calling – Critical error which is not acceptable in order until a try statement is found with a normal application program catch block for the exception • It is possible for a 
 • Exception Class catch statement to 
 – Possible exception in normal application defer handling of 
 program execution an exception by 
 – Possible to handle by programmer including a throw 
 statement of its 
 own

  5. � 17 � 18 Exception Class Hierarchy ตาวอย้าง System-Defined Exception Java has a predefined set of exceptions and errors • ArithmeticException : that can occur during execution. – When an exceptional arithmetic condition has occurred • ArrayStoreException : Exception (derived from Throwable ) – When assign object of incorrect type to element of array • FileNotFoundException : – เกิดขึ้นเมื่อมีการเปิดแฟ้มข้อมูล แต่หาแฟ้มที่ต้องการไม่พบ RunTimeException ClassNotFoundException IOException • IndexOutOfBoundsException : – When beyond the bound of index in the object which use index, ArithmeticException such as array, string, and vector ที้นิเสนอเป่นเพึยง • IllegalMonitorStateException : NullPointerException ตาวอย้างบางส้วนเท้านั๊น – When the thread which is not owner of monitor involves wait or notify method IndexOutOfBoundsException NumberFormatException � 19 � 20 Exception Occurrence Programmer-Defined Exception Exceptions raised by programmer • Raised implicitly by system • Raised explicitly by programmer • Check by compiler whether the – throw Statement exception handler for exception occurred exists or not throw ThrowableObject; – If there is no handler, it is error Throwable class or • Sub class of Exception class its sub class

  6. � 21 � 22 ตาวอย้าง System-Defined Exception Coding Exceptions • Try-Catch Mechanism • NegativeArraySizeException : – When using a negative size of array – Wherever your code may trigger an • NumberFormatException : exception, the normal code logic is placed – เกิดขณะที่ทําการแปลงสตริงไปเป็นข้อมูลชนิดตัวเลข แต่สตริงอยู่ในรูป inside a block of code starting with the แบบที่ไม่เหมาะสม “try” keyword: • NullPointerException : – After the try block, the code to handle the – When refer to object as a null pointer exception should it arise is placed in a • SecurityException : block of code starting with the “catch” – When violate security. Caused by security manager keyword. � 23 � 24 Coding Exceptions Exception Example • Try-Catch Mechanism • The body of a method may call other methods as well as doing its own calculations – You may also write an optional “finally” • Here the body of m will execute unless an out-of bounds block. This block contains code that is exception occurs void m (){ ALWAYS executed, either after the “try” try { block code, or after the “catch” block … body of m … code. } catch (ArrayIndexOutOfBoundsException ae) { – Finally blocks can be used for operations … code to recover from error … that must happen no matter what (i.e. } } cleanup operations such as closing a file)

Recommend


More recommend