10
CS12230 Introduction to Programming Lecture 6-2 –Errors and Exceptions 1

CS12230 Introduction to Programming Lecture 6-2 –Errors and Exceptions 1

Embed Size (px)

DESCRIPTION

Some run-time errors can be ‘caught’ When reading from a file do you remember ‘throws IOException’ which propagates up the calling sequence? This is sort of like saying that you recognise your code may have a problem Instead, You can ‘catch’ exceptions and deal with them: try{ //piece of code that might cause exception } catch (IOException e) {//or whatever exception //some code to fix problem } 3

Citation preview

Page 1: CS12230 Introduction to Programming Lecture 6-2 –Errors and Exceptions 1

CS12230Introduction to Programming

Lecture 6-2 –Errors and Exceptions

1

Page 2: CS12230 Introduction to Programming Lecture 6-2 –Errors and Exceptions 1

Errors

Three kinds:•Compiler – missing ;– Don’t recognise a variable or class name

•Run time– Something happens as you run– Often null pointers

•Logic– The program runs but not ‘right’

2

Page 3: CS12230 Introduction to Programming Lecture 6-2 –Errors and Exceptions 1

Some run-time errors can be ‘caught’

• When reading from a file do you remember ‘throws IOException’ which propagates up the calling sequence? This is sort of like saying that you recognise your code may have a problem

• Instead, You can ‘catch’ exceptions and deal with them:try{

//piece of code that might cause exception}catch (IOException e) { //or whatever exception

//some code to fix problem}

3

Page 4: CS12230 Introduction to Programming Lecture 6-2 –Errors and Exceptions 1

Look at the code in 6-week6

• BadApplication (next slide) and Application• Also summing etc.

4

Page 5: CS12230 Introduction to Programming Lecture 6-2 –Errors and Exceptions 1

5

Page 6: CS12230 Introduction to Programming Lecture 6-2 –Errors and Exceptions 1

Some run-time errors cannot and should not be caught

• They should be fixed instead• One really common one is ‘NullPointerException’• That means that you are trying to call a method

on an object that doesn’t existFor instance, in the code on last slide comment out staff=new ArrayList<Employee>();and see what happens

6

Page 7: CS12230 Introduction to Programming Lecture 6-2 –Errors and Exceptions 1

How to debug your code

Three kinds:•Compiler errors – usually not too hard with experience

•Run time– Try stopping execution just before the error

•Logic– Run line by line (in debugger or on paper)

Show the links on the web site (other resources)http://www.aber.ac.uk/~dcswww/Dept/Teaching/CourseNotes/2012-2013/CS12230/resources.htm

7

Page 8: CS12230 Introduction to Programming Lecture 6-2 –Errors and Exceptions 1

At 3pm

• Go on with adding ArrayLists to the Monsters project

8

Page 9: CS12230 Introduction to Programming Lecture 6-2 –Errors and Exceptions 1

Tomorrow

• I will hand back the test and talk about it at 3pm.

• Also answer any questions you have on the assignment – make sure you read the 6-3 slides if we don’t do them in class

• No 10am lecture (so I can finish the marking)

9

Page 10: CS12230 Introduction to Programming Lecture 6-2 –Errors and Exceptions 1

Next week is ‘work week’

• No CS lectures or Monday lab (demonstrators may be there)

• tutorials –you may persuade your leader to help you during that slot

• Yes, Wednesday and Friday practicals • This is your big opportunity to get caught up on reading

(how many of you have looked at the notes?) and practicals …

• … and make good start on assignment• I may be away for part of it but answering email

10