您的位置:首页 > 编程语言 > Java开发

Two Types of Error in JAVA

2016-10-05 03:20 483 查看
在 java 中有两种不同的 error, Compile-time Error 和 Runtime Error.

 

Compile-time Error:  发生在程序compile的时候, compile-time error 不能被catch,只是error,不是exception;程序并没有运行。

造成的原因:

  1. 语法错误 

          1.1  ...expected.  括号 ( parentheses, square brackets, curly braces)等没有匹配或者缺少分号( semicolon)  

          1.2unclosed string literal String 的冒号不匹配 。 

          1.3... 这里都是简单的语法上错误。 

  2. Identifiers 

          2.1  比如缺少对某个变量的定义  

          2.2  重复对某个变量定义  

          2.3 不能访问 ...has private access in ..

  3. Computation 

          3.1 使用没有被赋值的变量对其他变量赋值。 variable ... might not have been initialized. etc.  

          3.2 传入参数类型不匹配

          3.3 操作符不能进行计算 int a; boolean b; a + b 错误。  ..

  4. 返回值 

          4.1 缺少返回值/返回语句  

          4.2 没有返回类型 

          4.3 不能到达的语句 

  5. 访问静态entity

          访问对象中的方法, 没有实例化对象,也没有讲方法使用static进行标注。

Runtime Error: 发生在程序运行的时候. 

       Runtime Error 大体上可分为两类 - 1. Errors 2. Exceptions.

         它们的结构如下: (红色块 是checked exception 蓝色块的是 unckecked exception)

                Unchecked Exception: 就是不需要强制使用 try-catch throw的exception ( RunTime Exception 的所有子类)

                Checked Exception: Exception 的所有除了Runtime Exception 的子类,如果我们不对checked exception处理的时候,compile 根本过不了( 在eclipse上会显示exception,            
          并提示你增加try-catch)。

所以可以根据在编译的时候编译器是否会检查,检查的话就是checked exception, 反之就是 unchecked.

                                               


从上图可以看出来,Error 和 Exception 都是继承 Throwable 类, 那么说明他们都可以抛出异常。 但是值得注意的是Error不能被try - catch 到, 但是 Exception 能被try - catch 到。 

用户定义 Exception

以下是一个用户定义 exception 的例子。 

因为我们继承的是Exception, 所以我们定义的这个 exception 是 checked exception

package Exception_Example;

/**
* Define Exception Class
*
* */

public class InsufficientFundsException extends Exception{
private double amount;

public InsufficientFundsException (double amount) {
this.amount = amount;
}

public double getAmount () {
return amount;
}

}


package Exception_Example;

public class CheckingAccount {
private double balance;
private int number;

public CheckingAccount (int number) {
this.number = number;
}

public void deposit (double money) {
this.balance += money;
}

public void withdraw (double amount) throws InsufficientFundsException {
if (amount <= balance) {
balance -= amount;
} else {
double needs = amount - balance;
throw new InsufficientFundsException(needs);
}
}

public double getBalance () {
return this.balance;
}

public int getNumber () {
return this.number;
}
}


Main 函数

package Exception_Example;

public class BankDemo {
public static void main (String[] args) {
CheckingAccount account = new CheckingAccount(101);
account.deposit(500);
//Since we extends from Exception which is checked exception
//we must handle it before we can compile.
try{
account.withdraw(600);
} catch (InsufficientFundsException e) {
System.out.println(e + " Less money: " + e.getAmount());

} finally {
System.out.println("Successfully cache!");
}
}

}

因为这是一个 checked exception, 所以在main函数中我们必须加上try - catch. 

但是如果我们要是想定义一个unchecked 的 exception呢? 

其实就是简单的讲 InsufficientFundsException 的父类改成 RuntimeException, 在这时 main函数可以变成。并且不会报错( 运行时抛出异常)

<span style="font-size:14px;">package Exception_Example;

public class BankDemo {
public static void main (String[] args) {
CheckingAccount account = new CheckingAccount(101);
account.deposit(500);
account.withdraw(600);
}

}</span><span style="font-size:18px;">
</span>

有问题欢迎指出
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐