异常体系:
默认抛出runtimeException 事物回滚
抛出Exception error 事物不回滚
级别提升为throwable 不管Exception error事物都回滚
throwable 是所有类的父类 子类是error exception
throwable exception 需要抛出异常 或者try catch 捕获异常
error 和 runtimeException 是运行时异常 不需要处理,
分为4类:
Throwable throwable; 是一个class
需要处理异常
Error error; 是一个class 继承的是Throwable
Exception exception; 是一个class 继承的是Throwable
检查异常-当某个方法可能出现异常,需要处理,否则编译不通过。
RuntimeException runtimeException; 是一个class 继承的是Exception
运行时异常
自定义异常
public class Aoo extends Exception {
public Aoo(){
super();
}
public Aoo(String msg){
super(msg);
}
public Aoo (String msg,Throwable cause){
super(msg,cause);
}
public Aoo(Throwable cause){
super(cause);
}
}
public class Tests {
@Test
public void test() throws Aoo {
new Tests().a();
}
private void a() throws Aoo {
Aoo aoo = new Aoo();
throw aoo;
}
}
HashMap 继承的是runtimeException
初始化容量小于零,不合理运行时抛出异常
异常抓取:
private void a(){
try {
throw new Exception();
} catch (RuntimeException e) { //runtimeException是exceprtion的子类 是无法指向父类的
System.out.println(1);
}catch (Exception e){ //抓住异常后就不会再往下走
System.out.println(2);
}catch (Throwable e){
System.out.println(3);
}finally {
System.out.println(4);
}
}
输出2 4;
private void a(){
try {
int a=0;
throw new RuntimeException();
} catch (RuntimeException e) {
throw e;
*System.out.println(1);* 这段代码是编译不通过的,已经抛出异常不会再往下走。
}
}
finally 不一定会输出
public void a(){
if(1==1)
throw new RuntimeException();
try{
}finally {
System.out.println(4);
}
}
public int a(){
//n 储存的是值
int a =0;
try{
a=1;
return a;
}finally {
a=2;
return a;
}
}
返回2
public static Foo a(){
//n = foo 储存的是地址
Foo foo = new Foo();
try{
foo.setFlag("a");
return foo;
}finally {
foo.setFlag("b");
return foo;
}
}
断言 -ea开启断言 关闭以后可以不执行断言
//断言
boolean a=false;
assert a:"aa" ;
System.out.println(1);
public void a(){
try{
Exception e1= new Exception();
throw e1;
}catch (Exception e){
throw new RuntimeException("aa",e);
}
}
java.lang.RuntimeException: aa
at com.ssm.Tests.a(Tests.java:32)
at com.ssm.Tests.test(Tests.java:24)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: java.lang.Exception
at com.ssm.Tests.a(Tests.java:29)
... 23 more