110 likes | 236 Views
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
E N D
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 • Trying to read a file that does not exist • Trying to access a Web page does not exist
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
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)
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
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)
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)
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; }
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)
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;}
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.