Java语言

王晓蒙

目录

  • 1 Introduction to Java
    • 1.1 Programming Languages
    • 1.2 Java Introduction
    • 1.3 JDK Installation
    • 1.4 A Simple Program
    • 1.5 Programming Style and Documentation
    • 1.6 Programming Error
    • 1.7 Programming Using Eclipse
    • 1.8 Review Questions
  • 2 Elementary Programming
    • 2.1 Identifiers, Variables and Constants
    • 2.2 Input By Console
    • 2.3 Numeric Data Type and Literals
    • 2.4 Augmented Numeric Operators
    • 2.5 Numeric Type Conversions
    • 2.6 Mathematical Methods
    • 2.7 Character Data Type
    • 2.8 Using String Type
    • 2.9 Console Formatting Output
  • 3 Selections
    • 3.1 Boolean and Relational Operators
    • 3.2 if-else
    • 3.3 Logical Operators
    • 3.4 switch
  • 4 Loops
    • 4.1 while
    • 4.2 Case: Guessing Numbers
    • 4.3 do-while
    • 4.4 for
    • 4.5 Nested Loops
    • 4.6 break and continue
    • 4.7 Case: Checking Palindromes
  • 5 Methods
    • 5.1 Defining and Calling a Method
    • 5.2 Passing Arguments By Values
    • 5.3 Modularizing Code
    • 5.4 Overloading Methods
    • 5.5 Variables Scope
  • 6 Arrays
    • 6.1 Array Basics
    • 6.2 Case: Analyzing Numbers
    • 6.3 Copying Arrays
    • 6.4 Passing Arrays to Methods
    • 6.5 Variable-Length Arguments List
    • 6.6 The Arrays Class
    • 6.7 Two-Dimensional Arrays Basics
    • 6.8 Two-Dimensional Arrays Processing
    • 6.9 Multidimensional Arrays
  • 7 Objects and Classes
    • 7.1 Introduction to OOP
    • 7.2 Defining Classes and Creating Objects
    • 7.3 Constructors
    • 7.4 Reference Variables and Reference Types
    • 7.5 Using Java Library
    • 7.6 Static Members
    • 7.7 Visibility Modifers
    • 7.8 Passing Objects to Methods
    • 7.9 This Keyword
    • 7.10 Wrapper Class
    • 7.11 Immutable and Interned Strings
  • 8 Inheritance and Polymorphism
    • 8.1 Super-classes and Sub-classes
    • 8.2 super Keyword and Constructor Chaining
    • 8.3 Overriding
    • 8.4 Polymorphism
    • 8.5 Objects Casting
    • 8.6 The method equals
    • 8.7 The protected Members
  • 9 Abstract Classes and Interfaces
    • 9.1 Abstract Classes
    • 9.2 Interfaces
    • 9.3 Comparable Interface
  • 10 Exception Handling
    • 10.1 Introduction
    • 10.2 Exception Declaring and Throwing
    • 10.3 Exception Catching and Handling
    • 10.4 finally
  • 11 I/O Operation
    • 11.1 I/O Introduction
    • 11.2 Binary Input
    • 11.3 Binary Output
    • 11.4 Text Input
    • 11.5 Text Output
  • 12 JavaFX Basics
    • 12.1 A Basic JavaFX Program
    • 12.2 Node Types
    • 12.3 Property Binding
    • 12.4 Node Style
    • 12.5 Layout
    • 12.6 JavaFX and Eclipse
  • 13 Event-Driven Programming
    • 13.1 Event-Source, Event and Event-Handler
    • 13.2 Defining Event-Handler Using Inner-Class
    • 13.3 Event Handling Using Lambda Expression
  • 14 Others
Exception Declaring and Throwing


Exception Types

RuntimeExceptionError and their subclasses are known as unchecked exceptions. All other exceptions are known as checked exceptions, meaning that the compiler forces the programmer to check and deal with the exceptions. unchecked类型异常不需要进行异常处理,checked类型异常需要对异常进行捕获和处理。


Exception Declaring

若函数执行过程中可能抛出异常,则需要进行捕获或声明,例如这里的reader()函数中执行了FileInputStream构造器及其read()函数,通过查API知道这两个函数的调用可能抛出IOException和FileNotFoundException异常,如果不在reader()函数中对这两个函数进行捕获和处理,则需要声明,表示reader()函数会继续将这两种异常抛出。声明异常采用throws关键词,有多个异常则用逗号分离。


Exception Throwing

在设计函数时,业务中可能发生的异常进行判断,在异常点创建并抛出异常。抛出异常采用throw关键词,注意要和声明异常throws关键词进行区分。

这里在设计除法运算division()函数时,除数不能为0,当输出的除数为0时就可以抛出一个异常对象,这里Java API中已经存在专门针对算术计算的异常类,因此直接创建一个ArithmeticException对象,并用throw关键词抛出。通过异常处理的设计,使得上层应用代码必须去考虑处理该异常,从而增强程序的鲁棒性。


Declaring or Handling

抛出异常是将程序运行中发生的异常状态打包成一个对象抛出给上层代码(例如调用者),当发生异常后,上层代码(调用者)需要对checked类型的异常进行捕获和处理,如果不处理,则需要在当前层(当前调用者)对该类异常进行声明,异常会想冒泡一样继续抛给上层代码。

声明后继续抛出:

直接捕获并理: