当一个错误发生时,程序就会抛出异常. 异常列表可以参考下面的链接http://docs.oracle.com/javase/7/docs/api/java/lang/Exception.html.
异常由 try/catch 语句处理. 可能抛出异常的所有代码都必须遵循Catch或指定的要求. 按照这个要求, 这段代码必须包含在try语句块中. 如果由于某种原因,它不适合或不能使用try / catch语句,则必须指定所有异常的方法/函数可以使用 throws 关键字抛出
public void writeFile() throws IOException 
您也可以在代码中抛出一个新的异常::
throw new IllegalArgumentException("Number not above 0");
/* 将打印 
    Exception in thread "Main": java.lang.IllegalArgumentException: Number not above 0
*/
异常由 try/catch 语句处理, 这在前面已经学习:
try {
    System.out.println(arr[10]);
catch (ArrayIndexOutOfBoundsException ex) {
    System.out.println("Error in try block");
}
写一个程序抛出一个异常 IllegalArgumentException if (n < 0). 必须描述写为 Error.
public class Main {
    public static void main(String[] args) {
        int n = -1;
    }
}
//需要写出相似的描述.
Exception at thread "main": java.lang.IllegalArgumentException: Error
public class Main {
    public static void main(String[] args) {
        int n = -1;
        if (n < 0) {
            throw new IllegalArgumentException("Error")
        }
    }
}
原文:http://blog.csdn.net/tanxiang21/article/details/22049711