Logout

Home Topic 4 Last Next

4.1.13

Identify exceptions that need to be considered in a specified problem solution.

 

Teaching Note:

For example, identify the pre-conditions for calculating the end-of-year bonus when not all employees have worked for the company for the whole year.

LINK Connecting computational thinking and program design, introduction to programming.


 

Sample Question:

sdfsdfsf

JSR Notes:

 

A first important note is that this is an "identify" for a "specified" situation kind of assessment statement, so you can expect to be given a certain scenario, and with that particular situation, what are the particular exceptions which need to be considered.

 

Non-Computer Examples

For example, identify the pre-conditions for calculating the end-of-year bonus when not all employees have worked for the company for the whole year.

The exception is that if an employee has not worked for the company the whole year, he/she does not get a bonus. But the point is that that exception has to be "handled", i.e. the question has to be asked. You do not just go down through the list of employees at the end of the year and give them all the bonus.

Another example, what are the exceptions to be anticipated when running a IASAS soccer tournament?

- plan for what to do in case of soaked fields.... contact an indoor facility to be possibly used

- plan for what to do if one team cancels at the last moment... have an alternative schedule, or a B team from the host school ready to step in

- plan for what to do in case of major curfew violation... have an in-school suspension substitute teacher ready

etc.

 

Computer/Programming/Java Examples

***Note that though in the IB CS syllabus you don't need to know the coding approaches below, they are useful, particularly in your IA. And also, I add them here so that you know what we're talking about with regards to actual Java Exceptions.

So, in Java, we can handle exceptions three general ways.

  1. Simply by using if/else code
  2. By "throwing" and exception
  3. By using a try/catch block

if/else Example - a textField is left blank when a GUI button is clicked.

if(jTextFileld1.equals(""){
        JOptionPane.showMessageDialog(frame, "You need to fill in the text field.",  "Error",  JOptionPane.PLAIN_MESSAGE);
}

 

Throwing an Exception Example - a 0 sent as a parameter which is to be used as a divisor (i.e. denominator)... use Java IllegalArgumentException

if (divisor == 0) {     
          throw new IllegalArgumentException("Argument 'divisor' is 0"); 
}

 

try/catch Example - a file to be read is not in existence yet... use a Java FileNotFound Exception

try {
      myFile = FileReader("abcd.txt");
    }
    catch (FileNotFoundException ex)
    {
      System.out.println("main: Oops, FileNotFoundException caught");
    }