Presentation is loading. Please wait.

Presentation is loading. Please wait.

Exceptions Any number of exceptional circumstances may arise during program execution that cause trouble import java.io.*; class IOExample { public static.

Similar presentations


Presentation on theme: "Exceptions Any number of exceptional circumstances may arise during program execution that cause trouble import java.io.*; class IOExample { public static."— Presentation transcript:

1 Exceptions Any number of exceptional circumstances may arise during program execution that cause trouble import java.io.*; class IOExample { public static void main(String[] args) { try { BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in)); String line; System.out.print("Enter the number of fins > "); // wait til user enters something other than just RETURN while ((line=stdin.readLine()) == null) ; int nfins = Integer.valueOf(line).intValue(); } catch (IOException x) { // oops -- something strange went wrong System.out.println("Is your keyboard OK?! -- " + x); } catch (NumberFormatException x) { // entered something like “frog” System.out.println("Integers only! -- " + x); }

2 Exceptions Exceptions are objects that are thrown by code that encounters some sort of unexpected condition. They can also be thrown by the Java run-time environment. A thrown exception is caught by other code that handles the exception somehow, or the program is terminated unexpectedly.

3 Exceptions For instance, if we try to delete the tenth element from a sequence that has only five elements, the code may throw a BoundaryViolationException. if (insertIndex > size()) { throw new BoundaryViolationException(“No element at index” + insertIndex); }

4 Throw statement Throw new ([,, …]);

5 Kinds of Throwables Throwable ExceptionError RuntimeException

6 Catching Exceptions When an exception is thrown, it must be caught or the program will terminate. When an exception is caught, it can be analyzed and dealt with. The general methodology for dealing with exception is to “try” to execute some fragment of code that might throw an exception. If it does throw an exception, then that exception is caught by having the flow of control jump to a predefined catch block.

7 General syntax for a try-catch block try catch ( ) catch ( ) ……….. [finally ]

8  Lesson: Handling Errors with Exceptions If there's a golden rule of programming it's this: Errors occur in software programs. This we know. But what really matters is what happens after the error occurs. How is the error handled? Who handles it? Can the program recover, or should it just die? What's an Exception and Why Do I Care? The Java language uses exceptions to provide error-handling capabilities for its programs. An exception is an event that occurs during the execution of a program that disrupts the normal flow of instructions. Your First Encounter with Java Exceptions If you have done any amount of Java programming at all, you have undoubtedly already encountered exceptions. Your first encounter with Java exceptions was probably in the form of an error message from the compiler like this one: InputFile.java:11: Exception

9 java.io.FileNotFoundException must be caught, or it must be declared in the throws clause of this method. in = new FileReader(filename); ^ This message indicates that the compiler found an exception that is not being handled. The Java language requires that a method either catch all "checked" exceptions (those that are checked by the runtime system) or specify that it can throw that type of exception. Java's Catch or Specify Requirement This section discusses the reasoning behind this requirement and what it means to you and your Java programs. Dealing with Exceptions This section features an example program that can throw two different kinds of exceptions. Using this program, you can learn how to catch an exception and handle it and, alternatively, how to specify that your method can throw it.

10 Throwing Exceptions The Java runtime system and many classes from Java packages throw exceptions under some circumstances by using the throw statement. You can use the same mechanism to throw exceptions in your Java programs. This section shows you how to throw exceptions from your Java code. Runtime Exceptions--The Controversy Although Java requires that methods catch or specify checked exceptions, they do not have to catch or specify runtime exceptions, that is, exceptions that occur within the Java runtime system. Because catching or specifying an exception is extra work, programmers may be tempted to write code that throws only runtime exceptions and therefore doesn't have to catch or specify them. This is "exception abuse" and is not recommended. The last section in this lesson, explains why.


Download ppt "Exceptions Any number of exceptional circumstances may arise during program execution that cause trouble import java.io.*; class IOExample { public static."

Similar presentations


Ads by Google