You are previewing Java: The Complete Reference™, Eighth Edition.

Java: The Complete Reference™, Eighth Edition

Cover of Java: The Complete Reference™, Eighth Edition by Herbert Schildt Published by McGraw-Hill, Oracle Press
  1. Cover Page
  2. Java: The Complete Reference™
  3. Copyright Page
  4. About the Author
  5. Contents at a Glance
  6. Contents
  7. Preface
  8. PART I The Java Language
    1. CHAPTER 1 The History and Evolution of Java
      1. Java’s Lineage
      2. The Creation of Java
      3. How Java Changed the Internet
      4. Java’s Magic: The Bytecode
      5. Servlets: Java on the Server Side
      6. The Java Buzzwords
      7. The Evolution of Java
      8. A Culture of Innovation
    2. CHAPTER 2 An Overview of Java
      1. Object-Oriented Programming
      2. A First Simple Program
      3. A Second Short Program
      4. Two Control Statements
      5. Using Blocks of Code
      6. Lexical Issues
      7. The Java Class Libraries
    3. CHAPTER 3 Data Types, Variables, and Arrays
      1. Java Is a Strongly Typed Language
      2. The Primitive Types
      3. Integers
      4. Floating-Point Types
      5. Characters
      6. Booleans
      7. A Closer Look at Literals
      8. Variables
      9. Type Conversion and Casting
      10. Automatic Type Promotion in Expressions
      11. Arrays
      12. A Few Words About Strings
      13. A Note to C/C++ Programmers About Pointers
    4. CHAPTER 4 Operators
      1. Arithmetic Operators
      2. The Bitwise Operators
      3. Relational Operators
      4. Boolean Logical Operators
      5. The Assignment Operator
      6. The ? Operator
      7. Operator Precedence
      8. Using Parentheses
    5. CHAPTER 5 Control Statements
      1. Java’s Selection Statements
      2. Iteration Statements
      3. Jump Statements
    6. CHAPTER 6 Introducing Classes
      1. Class Fundamentals
      2. Declaring Objects
      3. Assigning Object Reference Variables
      4. Introducing Methods
      5. Constructors
      6. The this Keyword
      7. Garbage Collection
      8. The finalize( ) Method
      9. A Stack Class
    7. CHAPTER 7 A Closer Look at Methods and Classes
      1. Overloading Methods
      2. Using Objects as Parameters
      3. A Closer Look at Argument Passing
      4. Returning Objects
      5. Recursion
      6. Introducing Access Control
      7. Understanding static
      8. Introducing final
      9. Arrays Revisited
      10. Introducing Nested and Inner Classes
      11. Exploring the String Class
      12. Using Command-Line Arguments
      13. Varargs: Variable-Length Arguments
    8. CHAPTER 8 Inheritance
      1. Inheritance Basics
      2. Using super
      3. Creating a Multilevel Hierarchy
      4. When Constructors Are Called
      5. Method Overriding
      6. Dynamic Method Dispatch
      7. Using Abstract Classes
      8. Using final with Inheritance
      9. The Object Class
    9. CHAPTER 9 Packages and Interfaces
      1. Packages
      2. Access Protection
      3. Importing Packages
      4. Interfaces
    10. CHAPTER 10 Exception Handling
      1. Exception-Handling Fundamentals
      2. Exception Types
      3. Uncaught Exceptions
      4. Using try and catch
      5. Multiple catch Clauses
      6. Nested try Statements
      7. throw
      8. throws
      9. finally
      10. Java’s Built-in Exceptions
      11. Creating Your Own Exception Subclasses
      12. Chained Exceptions
      13. Three New JDK 7 Exception Features
      14. Using Exceptions
    11. CHAPTER 11 Multithreaded Programming
      1. The Java Thread Model
      2. The Main Thread
      3. Creating a Thread
      4. Creating Multiple Threads
      5. Using isAlive( ) and join( )
      6. Thread Priorities
      7. Synchronization
      8. Interthread Communication
      9. Suspending, Resuming, and Stopping Threads
      10. Obtaining A Thread’s State
      11. Using Multithreading
    12. CHAPTER 12 Enumerations, Autoboxing, and Annotations (Metadata)
      1. Enumerations
      2. Type Wrappers
      3. Autoboxing
      4. Annotations (Metadata)
    13. CHAPTER 13 I/O, Applets, and Other Topics
      1. I/O Basics
      2. Reading Console Input
      3. Writing Console Output
      4. The PrintWriter Class
      5. Reading and Writing Files
      6. Automatically Closing a File
      7. Applet Fundamentals
      8. The transient and volatile Modifiers
      9. Using instanceof
      10. strictfp
      11. Native Methods
      12. Problems with Native Methods
      13. Using assert
      14. Static Import
      15. Invoking Overloaded Constructors Through this( )
    14. CHAPTER 14 Generics
      1. What Are Generics?
      2. A Simple Generics Example
      3. A Generic Class with Two Type Parameters
      4. The General Form of a Generic Class
      5. Bounded Types
      6. Using Wildcard Arguments
      7. Creating a Generic Method
      8. Generic Interfaces
      9. Raw Types and Legacy Code
      10. Generic Class Hierarchies
      11. Type Inference with Generics
      12. Erasure
      13. Ambiguity Errors
      14. Some Generic Restrictions
  9. PART II The Java Library
    1. CHAPTER 15 String Handling
      1. The String Constructors
      2. String Length
      3. Special String Operations
      4. Character Extraction
      5. String Comparison
      6. Searching Strings
      7. Modifying a String
      8. Data Conversion Using valueOf( )
      9. Changing the Case of Characters Within a String
      10. Additional String Methods
      11. StringBuffer
      12. StringBuilder
    2. CHAPTER 16 Exploring java.lang
      1. Primitive Type Wrappers
      2. Void
      3. Process
      4. Runtime
      5. ProcessBuilder
      6. System
      7. Object
      8. Using clone( ) and the Cloneable Interface
      9. Class
      10. ClassLoader
      11. Math
      12. StrictMath
      13. Compiler
      14. Thread, ThreadGroup, and Runnable
      15. ThreadLocal and InheritableThreadLocal
      16. Package
      17. RuntimePermission
      18. Throwable
      19. SecurityManager
      20. StackTraceElement
      21. Enum
      22. ClassValue
      23. The CharSequence Interface
      24. The Comparable Interface
      25. The Appendable Interface
      26. The Iterable Interface
      27. The Readable Interface
      28. The AutoCloseable Interface
      29. The Thread.UncaughtExceptionHandler Interface
      30. The java.lang Subpackages
    3. CHAPTER 17 java.util Part 1: The Collections Framework
      1. Collections Overview
      2. JDK 5 Changed the Collections Framework
      3. The Collection Interfaces
      4. The Collection Classes
      5. Accessing a Collection via an Iterator
      6. Storing User-Defined Classes in Collections
      7. The RandomAccess Interface
      8. Working with Maps
      9. Comparators
      10. The Collection Algorithms
      11. Arrays
      12. Why Generic Collections?
      13. The Legacy Classes and Interfaces
      14. Parting Thoughts on Collections
    4. CHAPTER 18 java.util Part 2: More Utility Classes
      1. StringTokenizer
      2. BitSet
      3. Date
      4. Calendar
      5. GregorianCalendar
      6. TimeZone
      7. SimpleTimeZone
      8. Locale
      9. Random
      10. Observable
      11. Timer and TimerTask
      12. Currency
      13. Formatter
      14. The ResourceBundle, ListResourceBundle, and PropertyResourceBundle Classes
      15. Miscellaneous Utility Classes and Interfaces
      16. The java.util Subpackages
    5. CHAPTER 19 Input/Output: Exploring java.io
      1. The I/O Classes and Interfaces
      2. File
      3. The AutoCloseable, Closeable, and Flushable Interfaces
      4. I/O Exceptions
      5. Two Ways to Close a Stream
      6. The Stream Classes
      7. The Byte Streams
      8. The Character Streams
      9. The Console Class
      10. Serialization
      11. Stream Benefits
    6. CHAPTER 20 Exploring NIO
      1. The NIO Classes
      2. NIO Fundamentals
      3. Enhancements Added to NIO by JDK 7
      4. Using the NIO System
      5. Pre-JDK 7 Channel-Based Examples
    7. CHAPTER 21 Networking
      1. Networking Basics
      2. The Networking Classes and Interfaces
      3. InetAddress
      4. Inet4Address and Inet6Address
      5. TCP/IP Client Sockets
      6. URL
      7. URLConnection
      8. HttpURLConnection
      9. The URI Class
      10. Cookies
      11. TCP/IP Server Sockets
      12. Datagrams
    8. CHAPTER 22 The Applet Class
      1. Two Types of Applets
      2. Applet Basics
      3. Applet Architecture
      4. An Applet Skeleton
      5. Simple Applet Display Methods
      6. Requesting Repainting
      7. Using the Status Window
      8. The HTML APPLET Tag
      9. Passing Parameters to Applets
      10. getDocumentBase( ) and getCodeBase( )
      11. AppletContext and showDocument( )
      12. The AudioClip Interface
      13. The AppletStub Interface
      14. Outputting to the Console
    9. CHAPTER 23 Event Handling
      1. Two Event Handling Mechanisms
      2. The Delegation Event Model
      3. Event Classes
      4. The KeyEvent Class
      5. Sources of Events
      6. Event Listener Interfaces
      7. Using the Delegation Event Model
      8. Adapter Classes
      9. Inner Classes
    10. CHAPTER 24 Introducing the AWT: Working with Windows, Graphics, and Text
      1. AWT Classes
      2. Window Fundamentals
      3. Working with Frame Windows
      4. Creating a Frame Window in an Applet
      5. Creating a Windowed Program
      6. Displaying Information Within a Window
      7. Working with Graphics
      8. Working with Color
      9. Setting the Paint Mode
      10. Working with Fonts
      11. Managing Text Output Using FontMetrics
    11. CHAPTER 25 Using AWT Controls, Layout Managers, and Menus
      1. Control Fundamentals
      2. Labels
      3. Using Buttons
      4. Applying Check Boxes
      5. CheckboxGroup
      6. Choice Controls
      7. Using Lists
      8. Managing Scroll Bars
      9. Using a TextField
      10. Using a TextArea
      11. Understanding Layout Managers
      12. Menu Bars and Menus
      13. Dialog Boxes
      14. FileDialog
      15. Handling Events by Extending AWT Components
      16. A Word About Overriding paint( )
    12. CHAPTER 26 Images
      1. File Formats
      2. Image Fundamentals: Creating, Loading, and Displaying
      3. ImageObserver
      4. Double Buffering
      5. MediaTracker
      6. ImageProducer
      7. ImageConsumer
      8. ImageFilter
      9. Cell Animation
      10. Additional Imaging Classes
    13. CHAPTER 27 The Concurrency Utilities
      1. The Concurrent API Packages
      2. Using Synchronization Objects
      3. Phaser
      4. Using an Executor
      5. The TimeUnit Enumeration
      6. The Concurrent Collections
      7. Locks
      8. Atomic Operations
      9. Parallel Programming via the Fork/Join Framework
      10. The Concurrency Utilities Versus Java’s Traditional Approach
    14. CHAPTER 28 Regular Expressions and Other Packages
      1. The Core Java API Packages
      2. Regular Expression Processing
      3. Reflection
      4. Remote Method Invocation (RMI)
      5. Text Formatting
  10. PART III Software Development Using Java
    1. CHAPTER 29 Java Beans
      1. What Is a Java Bean?
      2. Advantages of Java Beans
      3. Introspection
      4. Bound and Constrained Properties
      5. Persistence
      6. Customizers
      7. The Java Beans API
      8. A Bean Example
    2. CHAPTER 30 Introducing Swing
      1. The Origins of Swing
      2. Swing Is Built on the AWT
      3. Two Key Swing Features
      4. The MVC Connection
      5. Components and Containers
      6. The Swing Packages
      7. A Simple Swing Application
      8. Event Handling
      9. Create a Swing Applet
      10. Painting in Swing
    3. CHAPTER 31 Exploring Swing
      1. JLabel and ImageIcon
      2. JTextField
      3. The Swing Buttons
      4. JTabbedPane
      5. JList
      6. JComboBox
      7. Trees
      8. JTable
      9. Continuing Your Exploration of Swing
    4. CHAPTER 32 Servlets
      1. Background
      2. The Life Cycle of a Servlet
      3. Servlet Development Options
      4. Using Tomcat
      5. A Simple Servlet
      6. The Servlet API
      7. The javax.servlet Package
      8. Reading Servlet Parameters
      9. The javax.servlet.http Package
      10. Handling HTTP Requests and Responses
      11. Using Cookies
      12. Session Tracking
  11. PART IV Applying Java
    1. CHAPTER 33 Financial Applets and Servlets
      1. Finding the Payments for a Loan
      2. Finding the Future Value of an Investment
      3. Finding the Initial Investment Required to Achieve a Future Value
      4. Finding the Initial Investment Needed for a Desired Annuity
      5. Finding the Maximum Annuity for a Given Investment
      6. Finding the Remaining Balance on a Loan
      7. Creating Financial Servlets
      8. Some Things to Try
    2. CHAPTER 34 Creating a Download Manager in Java
      1. Understanding Internet Downloads
      2. An Overview of the Download Manager
      3. The Download Class
      4. The ProgressRenderer Class
      5. The DownloadsTableModel Class
      6. The DownloadManager Class
      7. Compiling and Running the Download Manager
      8. Enhancing the Download Manager
    3. APPENDIX Using Java’s Documentation Comments
      1. The javadoc Tags
      2. The General Form of a Documentation Comment
      3. What javadoc Outputs
      4. An Example that Uses Documentation Comments
  12. Index
O'Reilly logo

CHAPTER 10Exception Handling

This chapter examines Java’s exception-handling mechanism. An exception is an abnormal condition that arises in a code sequence at run time. In other words, an exception is a runtime error. In computer languages that do not support exception handling, errors must be checked and handled manually—typically through the use of error codes, and so on. This approach is as cumbersome as it is troublesome. Java’s exception handling avoids these problems and, in the process, brings run-time error management into the object-oriented world.

Exception-Handling Fundamentals

A Java exception is an object that describes an exceptional (that is, error) condition that has occurred in a piece of code. When an exceptional condition ...

The best content for your career. Discover unlimited learning on demand for around $1/day.