Java中的异常Exception

 

涉及到异常类相关的文章:

(1)异常类不能是泛型的 http://www.cnblogs.com/extjs4/p/8888085.html

(2)Finally block may not complete normally的问题,参考文章:https://www.cnblogs.com/extjs4/p/9375400.html

(3)JLS https://docs.oracle.com/javase/specs/jls/se7/html/jls-11.html#jls-11.2 

(4)关于动态代理的异常 http://www.importnew.com/28664.html

 

try{ }catch(Exception e){ }

 上面的例子不会报错,虽然try块中不会抛出异常,所以catch中Exception或者Exception的父类时不要求try块中一定要抛出相应异常。   

Throwable为顶层父类,派生出Error类和Exception类,如下图:

错误:Error类以及他的子类的实例,代表了JVM本身的错误。错误不能被程序员通过代码处理,Error很少出现。因此,程序员应该关注Exception为父类的分支下的各种异常类。

关于断言的java.lang.AssertionError也是无法处理的,如果出现错误,程序将终止。开启断言可在Eclipse中的VM arguments中传入参数:-ea 进行开启

 

异常:Exception以及他的子类,代表程序运行时发送的各种不期望发生的事件。可以被Java异常处理机制使用,是异常处理的核心。而且如果这些异常没有被处理,会导致线程退出。异常又分为非检查异常(unckecked exception)与检查异常(checked exception)。

由上图可以看出,继承

例1:

class SubThread extends Thread  {
	@Override
	public void run() throws ArithmeticException{
		int i = 0;
		int x = i / i; // 抛出java.lang.ArithmeticException 
		System.out.println("SubThread end...");
	}

}  

运行时异常如果不处理最终还是会导致线程终止。

例2:

class SubThread extends Thread  {
	@Override
	public void run() throws ArithmeticException{
		int i = 0;
		try {
			int x = i / i; 
		} catch (Exception e) {
                    throw e;
		}
		System.out.println("SubThread end...");
	}
}  

抛出异常时没有具体进行处理,同样会导致线程终止。如果注释掉throw e语句后,则线程就可正常结束打印出SubThread end... 

 

11.2.1. Exception Analysis of Expressions

A class instance creation expression (§15.9) can throw an exception class E iff either:

  • The expression is a qualified class instance creation expression and the qualifying expression can throw E; or

  • Some expression of the argument list can throw E; or

  • E is determined to be an exception class of the throwsclause of the constructor that is invoked (§15.12.2.6); or

  • The class instance creation expression includes a ClassBody, and some instance initializer block or instance variable initializer expression in the ClassBody can throw E.

A method invocation expression (§15.12) can throw an exception class E iff either:

  • The method to be invoked is of the form Primary.Identifierand the Primary expression can throw E; or

  • Some expression of the argument list can throw E; or

  • E is determined to be an exception class of the throwsclause of the method that is invoked (§15.12.2.6).

For every other kind of expression, the expression can throw an exception class E iff one of its immediate subexpressions can throw E.

eg1:

 

class FirstException extends Exception {  }
 
class AA{
	public AA() throws FirstException{}
}
// 方法上必须有FirstException,否则下面抛出FirstException都将报错
public void dd() throws FirstException { 
	
	AA a = new AA(){}; // // throw FirstException
	
	Object b = new Object(){
		// instance variable initializer
		AA a = new AA(); // throw FirstException
		// instance initializer block
		{
			AA a = new AA(); // // throw FirstException
		}
	};
	
}

 

 

11.2.2. Exception Analysis of Statements

throw statement (§14.18) whose thrown expression has static type E and is not a final or effectively final exception parameter can throw E or any exception class that the thrown expression can throw.

For example, the statement throw new java.io.FileNotFoundException(); can throw java.io.FileNotFoundException only. Formally, it is not the case that it "can throw" a subclass or superclass of java.io.FileNotFoundException.

throw statement whose thrown expression is a final or effectively final exception parameter of a catch clause C can throw an exception class E iff:

  • E is an exception class that the try block of the trystatement which declares C can throw; and

  • E is assignment compatible with any of C's catchable exception classes; and

  • E is not assignment compatible with any of the catchable exception classes of the catch clauses declared to the left of C in the same try statement.

try statement (§14.20) can throw an exception class E iff either:

  • The try block can throw E, or an expression used to initialize a resource (in a try-with-resources statement) can throw E, or the automatic invocation of the close() method of a resource (in a try-with-resources statement) can throw E, and E is not assignment compatible with any catchable exception class of any catch clause of the try statement, and either no finally block is present or the finally block can complete normally; or

  • Some catch block of the try statement can throw E and either no finally block is present or the finally block can complete normally; or

  • finally block is present and can throw E.

An explicit constructor invocation statement (§8.8.7.1) can throw an exception class E iff either:

  • Some expression of the constructor invocation's parameter list can throw E; or

  • E is determined to be an exception class of the throwsclause of the constructor that is invoked (§15.12.2.6).

Any other statement S can throw an exception class E iff an expression or statement immediately contained in S can throw E.

 

 

关于异常的一些注意点:

(1)If a catch block handles more than one exception type, then the catch parameter is implicitly final.

(2)Rethrowing Exceptions with More Inclusive Type Checking(更具包容性的类型检查)

 

static class FirstException extends Exception {	}

static class SecondException extends Exception { }

// Java7之前对异常的处理
public void rethrowExceptionPriorSE7(String exceptionName) 
		throws Exception {
	try {
		if (exceptionName.equals("First")) {
			throw new FirstException();
		} else {
			throw new SecondException();
		}
	} catch (Exception e) {
		throw e;
	}
}	
// Java7新增对异常的处理语法
public void rethrowExceptionSE7(String exceptionName) 
		throws FirstException, SecondException {
	try {
		if (exceptionName.equals("First")) {
			throw new FirstException();
		} else {
			throw new SecondException();
		}
	} catch (Exception e) {
		throw e;
	}
}

 

In detail, in Java SE 7 and later, when you declare one or more exception types in a catch clause, and rethrow the exception handled by this catch block, the compiler verifies that the type of the rethrown exception meets the following conditions:

  • The try block is able to throw it.
  • There are no other preceding catch blocks that can handle it.
  • It is a subtype or supertype of one of the catch clause's exception parameters.

如上的三条对Java7及后面的版本来说都需要进行检查,如下:

static class FirstException extends Exception {	}

static class SecondException extends FirstException { }

// Java7新增对异常的处理语法
public void rethrowExceptionSE7(String exceptionName) 
		throws FirstException, SecondException {
	try {
		throw new FirstException();
	} catch (SecondException e) {
		throw e;
	}
}

在try块中抛出父类异常,在catch中捕获子类异常,这是允许的。   

 

 

 

 

 

 参考:

(1)http://www.importnew.com/26613.html

(2)https://docs.oracle.com/javase/specs/jls/se7/html/jls-11.html

(3)https://docs.oracle.com/javase/8/docs/technotes/guides/language/catch-multiple.html

 

转载于:https://www.cnblogs.com/extjs4/p/8645155.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值