Presentation is loading. Please wait.

Presentation is loading. Please wait.

CC1007NI: Further Programming Week 8-9 Dhruba Sen Module Leader (Islington College)

Similar presentations


Presentation on theme: "CC1007NI: Further Programming Week 8-9 Dhruba Sen Module Leader (Islington College)"— Presentation transcript:

1 CC1007NI: Further Programming Week 8-9 Dhruba Sen Module Leader (Islington College)

2 2.0

3 Main concepts to be covered Exception categories Defining new exceptions Throwing exceptions Simple file processing with exception handling

4 Exceptions Code can be protected with a try block: try { Protect one or more statements here. } catch(Exception exception) { Report and recover from the exception here. }

5 Exceptions try {... = Integer.parseInt(...);... } catch(NumberFormatException exception) {... } 1. Exception thrown from here 2. Control transfers to here

6 Exception categories Checked exceptions Subclasses of Exception Used for anticipated failures. Where recovery may be possible. Unchecked exceptions Subclasses of RuntimeException Used for unanticipated failures. Where recovery is unlikely.

7 Examples of Checked Exceptions IOException FileNotFoundException EOFException

8 Examples of Unchecked Exceptions NumberFormatException NullPointerException ArithmeticException IndexOutOfBoundsException ArrayIndexOutOfBoundsException

9 The exception class hierarchy

10 throw and throws keywords The throw keyword is used to throw a new exception: throw new NumberFormatException(“...”); The throws keyword is used to indicate that a method throws an exception: public static int parseInt(String s) throws NumberFormatException

11 Text input-output Input-output is particularly error-prone. It involves interaction with the external environment. The java.io package supports input-output. java.io.IOException is a checked exception.

12 Text output Use the FileWriter class. Open a file. Write to the file. Close the file. Failure at any point results in an IOException.

13 Text output try { FileWriter writer = new FileWriter("name of file"); while(there is more text to write) {... writer.write(next piece of text);... } writer.close(); } catch(IOException e) { something went wrong with accessing the file }

14 Text input Use the FileReader class. Augment with BufferedReader for line-based input. Open a file. Read from the file. Close the file. Failure at any point results in an IOException.

15 Text input try { BufferedReader reader = new BufferedReader( new FileReader("name of file ")); String line = reader.readLine(); while(line != null) { do something with line line = reader.readLine(); } reader.close(); } catch(FileNotFoundException e) { the specified file could not be found } catch(IOException e) { something went wrong with reading or closing }

16 2.0

17 Main concepts to be covered getActionCommand getSource

18 CurrencyConverter Buttons Two buttons are declared: private JButton euroButton; private JButton dollarButton; Text is placed on the buttons when they are created: euroButton = new JButton("Euros"); dollarButton = new JButton("Dollars");

19 getActionCommand The actionPerformed method of CurrencyConverter uses the getActionCommand method of the ActionEvent object to determine which button triggered the event. Alternatively, the getSource method of the ActionEvent object can be used. The getActionCommand returns the text written on the component whereas the getSource method returns the object that triggered the event.

20 actionPerformed using getActionCommand public void actionPerformed(ActionEvent event) { String command = event.getActionCommand(); if (command.equals("Euros")) { convertToEuros(); } else if (command.equals("Dollars")) { convertToDollars(); }

21 actionPerformed using getSource public void actionPerformed(ActionEvent event) { if (event.getSource() == euroButton) { convertToEuros(); } else if (event.getSource() == dollarButton) { convertToDollars(); }

22 2.0

23 Main concepts to be covered Casting numbers Casting objects

24 Casting numbers We can easily assign an int to a double because there is no possibility of a loss of precision In the following example, the value 3.0 will be assigned to d int i = 3; double d = i;

25 Casting numbers There would be a compilation error though if we tried to write: double d = 3.9; int i = d; If we want to assign a double to an int (or, for example, to a float) then we need to tell the compiler that we accept the possible loss of precision

26 Casting numbers This process is known as casting We need to cast the number to an int by writing (int) in front of it In the following example, the value 3 will be assigned to i double d = 3.9; int i = (int) d;

27 Casting objects Consider the dome-v2 project from Weeks 1 & 2 The CD class has the following method to return the number of tracks public int getNumberOfTracks() { return numberOfTracks; }

28 Casting objects Suppose we wanted to write a method in the Database class to return the number of tracks of a CD that occurred at a particular position in the array list The method might have the following signature public int getCDTracks(int index)

29 Casting objects The Item object returned from the get method would need to be cast into a CD object in order to call the getNumberOfTracks method public int getCDTracks(int index) { CD cd = (CD) items.get(index); return cd.getNumberOfTracks(); }

30 THANK YOU.


Download ppt "CC1007NI: Further Programming Week 8-9 Dhruba Sen Module Leader (Islington College)"

Similar presentations


Ads by Google