exceptions - COP 3330: Object-Oriented Programming Summer...

Info iconThis preview shows pages 1–7. Sign up to view the full content.

View Full Document Right Arrow Icon
COP 3330: Exception Handling Page 1 © Mark Llewellyn COP 3330: Object-Oriented Programming Summer 2007 Exception Handing in Java – Part 1 School of Electrical Engineering and Computer Science University of Central Florida Instructor : Mark Llewellyn markl@cs.ucf.edu HEC 236, 823-2790 http://www.cs.ucf.edu/courses/cop3330/sum2007
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
COP 3330: Exception Handling Page 2 © Mark Llewellyn Exception Handling in Java •A n exception is an abnormal event that occurs during program execution. – attempt to manipulate a nonexistent file. – improper array subscripting. – improper arithmetic operations such as divide by zero. • If an exception occurs and an exception-handler code segment is in effect for that exception, then flow of control is transferred to the handler. • If an exception occurs and there is no handler for it, the program terminates.
Background image of page 2
COP 3330: Exception Handling Page 3 © Mark Llewellyn Exception Handling in Java (cont.) import java.io.*; public class except_A { public static void main (String[] args) throws IOException { //get filename BufferedReader stdin = new BufferedReader( new InputStreamReader(System.in)); System.out.println(“Filename: “); String s = stdin.readLine(); //set up file stream for processing BufferedReader filein = new BufferedReader(new FileReader(s)); //extract values and perform calculation int numerator = Integer.parseInt(filein.readLine()); int denominator = Integer.parseInt(filein.readLine()); int quotient = numerator / denominator; System.out.println(); System.out.println(numerator + “ / “ + denominator + “ = “ + quotient); return; } }
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
COP 3330: Exception Handling Page 4 © Mark Llewellyn Exception Handling in Java (cont.) •T h e r e i s a throws expression in the main method signature for class except_A shown in the previous slide. – All of the interactive console application programs that have appeared in the notes this semester have included throws expressions in their main method signatures. • Java requires the throws expression for any method that does not handle the I/O exceptions that it may generate.
Background image of page 4
COP 3330: Exception Handling Page 5 © Mark Llewellyn Exception Handling in Java (cont.) • The inclusion of the throws expression is a warning to users of the method. Such knowledge is important because if an invoked method does not handle an exception, then the exception is given to the invoking method to handle. – If the invoking method does not handle the exception, then the unwinding process continues with the method that did the invoking of the invoking method, and so on. If no method is found in the unwinding process to handle the exception, then the program terminates.
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Exception Handling Page 6 © Mark Llewellyn Exception Handling in Java (cont.) • Suppose that program except_A is executed and the user specifies the file to be the file named numbers.txt containing the values 35 and 7 on successive lines. The I/O behavior of the program is shown below. User supplies a valid
Background image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 47

exceptions - COP 3330: Object-Oriented Programming Summer...

This preview shows document pages 1 - 7. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online