1 / 11

Exceptions

Exceptions. Java API. Exceptions. Exceptions – abnormal or erroneous situations: Trying to dividing a number by zero Trying to convert a string containing non-digit character into a number Trying to access an element of an array past its maximum index

wilmer
Télécharger la présentation

Exceptions

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. Exceptions Java API

  2. Exceptions • Exceptions – abnormal or erroneous situations: • Trying to dividing a number by zero • Trying to convert a string containing non-digit character into a number • Trying to access an element of an array past its maximum index • Trying to read a file that does not exist • Trying to access a Web page does not exist

  3. Exception Handler • Exception Handler – code that is executed when an exception is detected • In Java, exception handler portion is clearly separated from the code for normal situations • An exception may be handled within the method where it is found, or in a different section where it is more appropriate

  4. Example: BMI • (weight in pounds)BMI = 703 x --------------------- (height in inches)2 • BMI Table • BMI Page(http://www.cdc.gov/nccdphp/dnpa/bmi/calc-bmi.htm)

  5. Applet to Calculate BMI • ExceptionDemo (with no exception handler) • ExceptionDemo.java source code • Possible Exceptions • double bmi = 703 * pounds / ((inches)*(inches));ArithmeticException • double inches = Double.parseDouble(tfInches.getText());NumberFormatExcepton

  6. Trying and Catching Exceptions public void actionPerformed(ActionEvent e) { if (e.getSource() == btCalculate) { // code for normal case try{ // code section that can cause exceptions } catch(NumberFormatException exc) { // code for exception handler } } • ExceptionDemo2.java source code (with exception handler)

  7. General Form for Exception Handler public void someMethod { // Here, code for normal case . . . try{ // code section that can cause exceptions } catch(NumberFormatException exc) { // code for exception handler } } • ExceptionDemo2.java source code (with exception handler)

  8. Example: Finding an Average • Suppose you have the following code segment: public double aveOfFirstN(int n) { int sum = 0; for (int i = 0; i <= n; i++) sum += i; return sum / n; }

  9. Example: Finding an Average (cont.) • Note that expression sum/n can raise an exception if n==0. The system will output the following message:Excepton in thread "main" java.lang.ArithmeticException: / by zero at AverageTest.aveOfFirstN(AverageTest.java: 6) at AverageTest.main(AverageTest.java: 12)

  10. Example: Finding an Average (cont.) • The following modification checks the value of n before calculating the average. public double aveOfFirstN(int n) { if (n > 0) { int sum = 0; for (int i = 0; i <= n; i++) sum += i; return sum / n; } else return -1.0;}

  11. Example: Finding an Average (cont.) • Note that some special value, -1 in this case, must be returned when n==0. • That means that the method that called the aveOfFirstN() must be aware of the special value. • See AverageTest1 source code. • A more elegant solution is to incorporate an exception handler. • See AverageTest2 source code.

More Related