在java中异常是定义了在程序中遇到非致命性的错误的问题。比如说除数不能为零,网络中断问题。
先来看一下异常的体系。
Trowable是异常的超类,它有两个子类分别是Error、Exception
Error:这个类是处理程序中出现严重性问题。
Exception:这个类及其子类是处理程序中出现的非严重性的问题。它也可以分为两大类:
1、执行时异常。(RuntimeException):这个异常是在执行时出现抛出但未捕获的该异常及其子类。(可捕获,可不捕获)
2、检查时异常(Checked Exceptions):这个异常是编译器要求程序必须捕获的异常或抛出异常。
public class Test{
public static void main(String[] args){
devide(3,0);
System.out.println("我执行了");
}
public static int devide(int num1,int num2){//求两个数的商
return num1/num2;
}
}
运行时结果:
Exception in thread "main" java.lang.ArithmeticException: / by zero
at Test.devide(Test.java:6)
at Test.main(Test.java:3)
报出ArithmeticException异常,原因是除数不能为零。那怎么去处理这个异常呢?
public class Test{
public static void main(String[] args){
try {
devide(3, 0);
} catch (ArithmeticException e) {
System.out.println("异常出错原因 :"+e.getMessage());;
}
System.out.println("我执行了");
}
public static int devide(int num1,int num2){//求两个数的商
return num1/num2;
}
}
运行结果:
异常出错原因 :/ by zero
我执行了
经过上面的程序处理可以看出,通过处理之后程序可以运行catch后面的语句。没有因为一个程序异常出错而导致崩溃。这也说明了异常的重要性和异常的好处。
总结:try{}catch{}是处理异常语句,try{需要检测的代码}catch{对try语句中被检测的代码出现异常而进行的处理}
import java.io.*;
public class Test{
public static void main(String[] args){
BufferedReader bur=new BufferedReader(new InputStreamReader(System.in));
String line=null;
try {
while((line=bur.readLine())!=null){
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}finally{
if(bur!=null){
try {
bur.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
在IO流中不管程序出现什么异常,最后都要关闭与之相关的所有资源。在上面程序中finally{}块中处理释放资源的操作,那么就可以知道finally{}块有什么作用了:finally{一定会执行的代码}
假如定义了一个有异常的方法(如下程序)并且抛给了上一层,其他程序再调用这个方法时只有两种处理方法,要么try{}catch{}要么继续抛给上一层,直至抛给虚拟机,让虚拟机处理。就好比一个学生让另一个学生给他做题目,而另一个同学在做题中特意留一些错误,并告诉他哪里出错了,他要么自己处理这些错误,要么交给老师,让老师来处理如同下面程序 throw new Exception()是引出错误,throws Excepiton是告诉上一层我这个方法有错误,你要小心点。
public class Test{
public static void main(String[] args){
try {
int num=devide(3,0);
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
public static int devide(int num1,int num2) throws Exception{
if(num2==0){
throw new Exception("我又抛出新的异常");
}
return num1/num2;
}
}
上面程序出现throws和throw两个关键字,下面来总结这两个关键字有什么不同?
throw和throws的不同点:
thorw是在方法里后面跟的是异常对象。
throws是跟在方法上后面跟的是异常类,并且可以跟多个用逗号隔开的异常类。
下面来看两个程序:
程序1:
public class Test{
public static void main(String[] args) {
new Thread(new Runnable(){
public void run() throws Exception{
for(int i=0;i<4;i++){
if(i%2==0)
throw new Exception("Exceptions");
}
}
}).start();
}
}
程序2:
public class Test{
public static void main(String[] args) {
new Thread(new Runnable(){
public void run(){
for(int i=0;i<4;i++){
if(i%2==0)
try {
throw new Exception("Exceptions");
} catch (Exception e) {
// TODO Auto-generated catch block
System.out.println(e);
}
}
}
}).start();
}
}
程序1编译时就报错,程序2打印的结果是:
java.lang.Exception: Exceptions
java.lang.Exception: Exceptions
在前面说过处理异常有两种方法,而在再看程序1将异常抛给上一层却不行了,只有通过try{}catch(){}语句处理异常。那这又是为什么呢?
程序2 new Runnable(){}是一个匿名内部类,子类重写了接口中run()方法,通过查阅API文档可知接口中的run()方法是不抛出任何异常,而在子类重写的时候抛出了Exception异常,而将异常抛给上一层的时候程序却出错。这说明子类是不能抛出比父类更多的异常。
自定义异常类:只要一个类继承了Exception类,那么这个类就是异常类。如下程序:
public class AException extends Exception {
private static final long serialVersionUID = -470617783876574106L;
public AException() {
super();
}
public AException(String message,Throwable cause) {
super(message, cause);
}
public AException(String message) {
super(message);
}
public AException(Throwable cause) {
super(cause);
}
}
public class Test {
public static void main(String[] args){
A a=new A();
try {
a.test();
} catch (AException e) {
System.out.println(e.getMessage());
}
}
}
class A {
public void test() throws AException{
throw new AException("自定义的异常类");
}
}
运行结果:自定义的异常类
----------------------
android培训、java培训、期待与您交流! ----------------------