Java异常error回滚吗_java异常体系

异常体系:

默认抛出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

初始化容量小于零,不合理运行时抛出异常

cf9eb1a71cb1964ae082d4502f172094.png

04df80cdd589287ff8e00d0b9c62fbeb.png

异常抓取:

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
必须制定一系列的规范,以确保网络设计的质量和效果。网络设计规范包括要在Java回滚数据,你需要使用Java中的事务(Transaction)概念。当一个事务失败,所有以下方面: 1. 网络架构设计规范 网络架构设计是网络设计的核心,必须根据的操作都应该回滚,以确保数据的一致性。在Java中,你可以使用JDBC或者J网络规划设计报告,制定出适合用户的网络架构设计方案。网络架构设计规范包括PA等ORM框架来实现事务。 在JDBC中,你可以使用以下代码来实现事务回滚: (1)分层设计原则:网络架构设计必须遵循分层设计原则,将网络划分为: ``` try { // 开始事务 connection.setAutoCommit(false); // 执行SQL语句 // ... 不同的层次,每个层次有不同的功能。 (2)模块化设计原则:网络架构 // 提交事务 connection.commit(); } catch (SQLException e) { // 回滚事务 connection.rollback(); 设计必须遵循模块化设计原则,将网络划分为不同的模块,每个模块有不 throw e; } ``` 在JPA中,你可以使用以下代码来实现事务回滚: ``` try { 同的功能。 2. 网络设备配置规范 网络设备配置是网络设计的重要环节,必须 entityManager.getTransaction().begin(); // 执行JPA操作 // ... entityManager.getTransaction().commit(); } catch (Exception e)根据网络架构设计方案,选择适合的网络设备,并进行配置。网络设备配置规范包括: { entityManager.getTransaction().rollback(); throw e; } ``` 注意,无论是JDBC还是JPA,都需要在catch块中调用回滚操作,以确保在发生异常回滚事务

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值