17slide_2007 - Chapter 17 Exceptions and Assertions...

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

View Full Document Right Arrow Icon
1 Chapter 17 Chapter 17 Exceptions and Assertions Exceptions and Assertions
Background image of page 1

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

View Full DocumentRight Arrow Icon
2 Objectives Objectives To know what is exception and what is exception handling. To distinguish exception types: Error (fatal) vs. Exception (non-fatal), and checked vs. uncheck exceptions. To declare exceptions in the method header. To throw exceptions out of a method. To write a try-catch block to handle exceptions. To explain how an exception is propagated. To rethrow exceptions in a try-catch block. To use the finally clause in a try-catch block. To know when to use exceptions. To declare custom exception classes. To apply assertions to help ensure program correctness.
Background image of page 2
3 Syntax Errors, Runtime Errors, and Syntax Errors, Runtime Errors, and Logic Errors Logic Errors You learned that there are three categories of errors: Syntax errors arise because the rules of the language have not been followed. They are detected by the compiler. Runtime errors occur while the program is running if the environment detects an operation that is impossible to carry out. Logic errors occur when a program doesn't perform the way it was intended to.
Background image of page 3

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

View Full DocumentRight Arrow Icon
4 Exceptions Exceptions The basic philosophy of Java is that “badly formed code will not be run.” The ideal time to catch an error is at compile time, before you even try to run the program. However, not all errors can be detected at compile time. The rest of the problems must be handled at run time through some formality that allows the originator of the error to pass appropriate information to a recipient who will know how to handle the difficulty properly. – Java provides a mechanism, exception handling, as the formal way to handle errors In general, reading, writing, and debugging code becomes much clearer with exceptions
Background image of page 4
5 Without Catching Runtime Errors Without Catching Runtime Errors import java.util.Scanner; public class ExceptionDemo { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print( "Enter an integer: " ); int number = scanner.nextInt(); // Display the result System.out.println( "The number entered is " + number); } } If an exception occurs on this line, the rest of the lines in the method are skipped and the program is terminated. Terminated.
Background image of page 5

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

View Full DocumentRight Arrow Icon
Image of page 6
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 01/11/2011 for the course EE 3206 taught by Professor Cwting during the Spring '07 term at City University of Hong Kong.

Page1 / 44

17slide_2007 - Chapter 17 Exceptions and Assertions...

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

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