Java 异常创建及控制

最近在重新拾起Java,想开始分享一些自己的表达,就从这里开始了。

Java中有一个Throwable类,它是所有异常或者说是违例的基础,包括了两种类型的异常:一种叫Error,表示的是编译器和系统错误,我们通常不需要去在意它们。另一种叫Exception,运行时的异常。接下来会介绍异常的创建和捕获及处理。

1、创建异常:

我们知道对于异常的捕获可以帮助我们有效地解决麻烦,但有时候不一定要用Java的异常类,我们经常需要根据具体的情况创建自己的异常,在创建异常类时,需要继承一个已经有的异常类。

这里顺带提及下throw和throws的作用:throw关键字的作用是抛出一个异常对象,通常在程序员需要主动抛出某个异常时使用;throws表示方法可能抛出异常的声明,语法如下:public void test() throws Exception { } 。

创建异常类:

import java.util.*;

class MyException extends Exception {
	private int i;

	public MyException() {
	};

	public MyException(String msg, int x) {
		super(msg);
		i = x;
	}

	public int val() {
		return i;
	}
}

public class Inheriting {
	public static void f() throws MyException {
		System.out.println("Throwing MyException from f()");
		throw new MyException();
	}

	public static void g() throws MyException {
		System.out.println("Throwing MyException from g()");
		throw new MyException("Originated in g()", 12);
	}

	public static void main(String[] args) {
		try {
			f();
		} catch (MyException e) {
			e.printStackTrace();
		}
		try {
			g();
		} catch (MyException e) {
			e.printStackTrace();
			System.out.println("e.val() = " + e.val());
		}
	}
}

如上,输出结果是:

Throwing MyException from f()

exceptioncontrol.MyException

at exceptioncontrol.Inheriting.f(Inheriting.java:24)
at exceptioncontrol.Inheriting.main(Inheriting.java:34)

Throwing MyException from g()

e.val() = 12

exceptioncontrol.MyException: Originated in g()
at exceptioncontrol.Inheriting.g(Inheriting.java:29)
at exceptioncontrol.Inheriting.main(Inheriting.java:39)

2、捕捉异常及控制

我们通常使用try { }catch(){ } 块来对异常进行捕获,try{ }中写入需要进行异常控制的代码,catch用于对指定类型的异常的捕获。如:

package exceptioncontrol;
public class Rethrowing {
	public static void f() throws Exception {
		System.out.println("originating the exception in f()");
		throw new Exception("throw from f()");
	}

	public static void g() throws Throwable {
		try {
			f();
		} catch (Exception e) {
			System.out.println("Inside g(), e.printStackTrace()");
			e.printStackTrace();
			throw e;
		}
	}

	public static void main(String[] args) throws Throwable {
		try {
			g();
		} catch (Exception e) {
			System.out.println("Caught in main, e.printStackTrace()");
			e.printStackTrace();
		}
	}
}
输出语句如下:

originating the exception in f()
Inside g(), e.printStackTrace()
java.lang.Exception: thrown from f()
at exceptioncontrol.Rethrowing.f(Rethrowing.java:6)
at exceptioncontrol.Rethrowing.g(Rethrowing.java:11)
at exceptioncontrol.Rethrowing.main(Rethrowing.java:21)
Caught in main, e.printStackTrace()
java.lang.Exception: thrown from f()
at exceptioncontrol.Rethrowing.f(Rethrowing.java:
6)
at exceptioncontrol.Rethrowing.g(Rethrowing.java:11)
at exceptioncontrol.Rethrowing.main(Rethrowing.java:21)

这里对异常进行了捕获,其中用到了一个方法,printStackTrace(),调用这个方法将会打印出标准错误,并且显示出违例发生地点的方法调用的顺序。

在代码中,可以看到,g()方法在捕获异常时,重新将所捕获的异常进行抛出,若仔细看输出结果会发现两次抛出的异常都进行跟踪到起点,这样可以打印出完整的堆栈跟踪信息,当然,可以使用fillInStackTrace(),它会返回新的堆栈跟踪信息。重新抛出异常可以获取在路径上所有可能发生的异常,在某些特定情况下,这是必须的。

这篇大概就写这么多,对于finally和异常匹配在下一篇在进行介绍,若有错误之处,欢迎指出,谢谢。


  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值