CS 2334: Lab 5 Exceptions Andrew H. Fagg: CS2334: Lab 5 1 - - PowerPoint PPT Presentation

cs 2334 lab 5
SMART_READER_LITE
LIVE PREVIEW

CS 2334: Lab 5 Exceptions Andrew H. Fagg: CS2334: Lab 5 1 - - PowerPoint PPT Presentation

CS 2334: Lab 5 Exceptions Andrew H. Fagg: CS2334: Lab 5 1 Exceptions Remember when an Exception is thrown: The normal execution of code stops The JVM begins to search the call stack for a catch statement that matches the Exception


slide-1
SLIDE 1

CS 2334: Lab 5 Exceptions

Andrew H. Fagg: CS2334: Lab 5 1

slide-2
SLIDE 2

Exceptions

Remember – when an Exception is thrown:

  • The normal execution of code stops
  • The JVM begins to search the call stack for a catch statement

that matches the Exception

  • This search can extend across methods
  • If a matching catch is found:
  • The catch block is executed
  • Execution then proceeds after the try/catch that caught the

exception

Andrew H. Fagg: CS2334: Lab 5 2

slide-3
SLIDE 3

Exceptions

  • If a matching catch is found:
  • The catch block is executed
  • Execution then proceeds after the try/catch that caught the

exception

  • If a match is not found in the call stack, the program halts

Andrew H. Fagg: CS2334: Lab 5 3

slide-4
SLIDE 4

Throwable Cla lass: Key Methods

  • Return the message associated with the Exception:

public String getMessage()

  • Return information about the Exception (includes the

message): public String toString()

  • Print out the entire stack up to the point where the

Exception was thrown: public void printStackTrace()

Andrew H. Fagg: CS2334: Lab 5 4

slide-5
SLIDE 5

Example

Recall from lecture:

static int getIntFromUser(BufferedReader br) throws IOException{ String strg = br.readLine(); int i = Integer.parseInt(strg); return i; }

  • Return an entered int or throw an Exception
  • NumberFormatException: user entered something other

than a number

Andrew H. Fagg: CS2334: Lab 5 5

slide-6
SLIDE 6

Example: Receiving Two In Ints and Dividing One by the Other

public static void main(String[] args) throws IOException{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int val1, val2, result; System.out.println("Please enter a pair of numbers on separate lines."); val1 = getIntFromUser(br); val2 = getIntFromUser(br); result = val1/val2; System.out.println("Result: " + result); }

What can go wrong? How do we fix it?

Andrew H. Fagg: CS2334: Lab 5 6

slide-7
SLIDE 7

boolean flag = true; try{ do{ try{ System.out.println("Please enter a pair of …”); val1 = getIntFromUser(br); val2 = getIntFromUser(br); result = val1/val2; flag = false; }catch(NumberFormatException e){ System.out.println("Numbers only!"); } }while(flag); System.out.println("Result: " + result); }catch(ArithmeticException e){ System.out.println("Divide by zero error."); System.exit(0); }

Andrew H. Fagg: CS2334: Lab 5 7

slide-8
SLIDE 8

boolean flag = true; try{ do{ try{ System.out.println("Please enter a pair of …”); val1 = getIntFromUser(br); val2 = getIntFromUser(br); result = val1/val2; flag = false; }catch(NumberFormatException e){ System.out.println("Numbers only!"); } }while(flag); System.out.println("Result: " + result); }catch(ArithmeticException e){ System.out.println("Divide by zero error."); System.exit(0); }

Andrew H. Fagg: CS2334: Lab 5 8

Loop until a pair of valid ints is entered

slide-9
SLIDE 9

boolean flag = true; try{ do{ try{ System.out.println("Please enter a pair of …”); val1 = getIntFromUser(br); val2 = getIntFromUser(br); result = val1/val2; flag = false; }catch(NumberFormatException e){ System.out.println("Numbers only!"); } }while(flag); System.out.println("Result: " + result); }catch(ArithmeticException e){ System.out.println("Divide by zero error."); System.exit(0); }

Andrew H. Fagg: CS2334: Lab 5 9

Do the work: receive two ints and divide

slide-10
SLIDE 10

boolean flag = true; try{ do{ try{ System.out.println("Please enter a pair of …”); val1 = getIntFromUser(br); val2 = getIntFromUser(br); result = val1/val2; flag = false; }catch(NumberFormatException e){ System.out.println("Numbers only!"); } }while(flag); System.out.println("Result: " + result); }catch(ArithmeticException e){ System.out.println("Divide by zero error."); System.exit(0); }

Andrew H. Fagg: CS2334: Lab 5 10

If we get here, then we are successful

slide-11
SLIDE 11

boolean flag = true; try{ do{ try{ System.out.println("Please enter a pair of …”); val1 = getIntFromUser(br); val2 = getIntFromUser(br); result = val1/val2; flag = false; }catch(NumberFormatException e){ System.out.println("Numbers only!"); } }while(flag); System.out.println("Result: " + result); }catch(ArithmeticException e){ System.out.println("Divide by zero error."); System.exit(0); }

Andrew H. Fagg: CS2334: Lab 5 11

If one of the ints is a problem, then we will print error and repeat

slide-12
SLIDE 12

boolean flag = true; try{ do{ try{ System.out.println("Please enter a pair of …”); val1 = getIntFromUser(br); val2 = getIntFromUser(br); result = val1/val2; flag = false; }catch(NumberFormatException e){ System.out.println("Numbers only!"); } }while(flag); System.out.println("Result: " + result); }catch(ArithmeticException e){ System.out.println("Divide by zero error."); System.exit(0); }

Andrew H. Fagg: CS2334: Lab 5 12

If successful, then stop looping

slide-13
SLIDE 13

boolean flag = true; try{ do{ try{ System.out.println("Please enter a pair of …”); val1 = getIntFromUser(br); val2 = getIntFromUser(br); result = val1/val2; flag = false; }catch(NumberFormatException e){ System.out.println("Numbers only!"); } }while(flag); System.out.println("Result: " + result); }catch(ArithmeticException e){ System.out.println("Divide by zero error."); System.exit(0); }

Andrew H. Fagg: CS2334: Lab 5 13

Print result of division

slide-14
SLIDE 14

boolean flag = true; try{ do{ try{ System.out.println("Please enter a pair of …”); val1 = getIntFromUser(br); val2 = getIntFromUser(br); result = val1/val2; flag = false; }catch(NumberFormatException e){ System.out.println("Numbers only!"); } }while(flag); System.out.println("Result: " + result); }catch(ArithmeticException e){ System.out.println("Divide by zero error."); System.exit(0); }

Andrew H. Fagg: CS2334: Lab 5 14

Catch the divide by zero error. In this case, don’t prompt again

slide-15
SLIDE 15

Lab 5: : Calculator with Error Checking

Loop:

  • Prompt user to enter a mathematical operator and one/two
  • perands (parameters)
  • Perform the operation with the parameters
  • Print the result
  • Quit when told to

Andrew H. Fagg: CS2334: Lab 5 15

slide-16
SLIDE 16

Operations

Addition: 1 x y Subtraction: 2 x y Multiplication: 3 x y Division: 4 x y Power: 5 x y Factorial: 6 x Quit: 7

Andrew H. Fagg: CS2334: Lab 5 16

slide-17
SLIDE 17

Error Checking

Must address:

  • Illegal operator
  • Illegal parameters
  • Incorrect number of parameters

Andrew H. Fagg: CS2334: Lab 5 17

slide-18
SLIDE 18

Lab 5 Im Implementation

Andrew H. Fagg: CS2334: Lab 5 18

slide-19
SLIDE 19

Class: CalculatorException

  • Extends Exception
  • Adds one more instance variable: exitFlag
  • True if the program should exit
  • Constructors:
  • String message
  • String message and exitFlag
  • Appropriate getters
  • Note that Exception already provides one that you will need

Andrew H. Fagg: CS2334: Lab 5 19

slide-20
SLIDE 20

CalculatorOperations

  • Provides static method for performing a single operation
  • Input: array of Strings
  • 0: String containing operator
  • 1: first parameter (if needed)
  • 2: second parameter (if needed)
  • Output: result of executing operator on parameters
  • Throws: CalculatorException
  • If there is an error in interpreting the inputs
  • If the number of inputs is incorrect
  • If the user has specified that the program should exit

Andrew H. Fagg: CS2334: Lab 5 20

slide-21
SLIDE 21

Driver: main()

Loop:

  • Receive a line of input from the user
  • Split the line into substrings (spaces separate the items)
  • Calls method to perform calculation
  • Addresses any Exceptions that might be thrown

Andrew H. Fagg: CS2334: Lab 5 21

slide-22
SLIDE 22
  • Demonstration…

Andrew H. Fagg: CS2334: Lab 5 22

slide-23
SLIDE 23

Im Implementation Process

  • Implement Driver and the CalculatorOperations classes first

assuming that there are no Exceptions to address

  • Implement CalculatorOperationsTest (a JUnit test)
  • Implement CalculatorException and CalculatorExceptionTest
  • Modify the Driver and CalculatorOperations classes to

address exceptions

  • performOperation() must only throw CalculatorExceptions
  • Driver.main() must only catch CalculatorExceptions

Andrew H. Fagg: CS2334: Lab 5 23

slide-24
SLIDE 24

Submission

  • Submit only one file: lab5.zip (casing matters)
  • Due date: Friday, September 25th @11:59pm
  • Submit to lab5 dropbox on D2L

Andrew H. Fagg: CS2334: Lab 5 24