Recently I am prepare an internal training and have been racking my brains to find a real example for my attendees about writting a “correct” program which gets rejected by compiler. The tricky point here is as a programmer, we always treat compiler as our god: if compiler complains that our program has errors, then we are wrong. Programmers tend to believe in that compiler will NEVER make mistakes.

Checked and unchecked exception in Java

Let’s see the following Java code:

package exception;
import java.sql.SQLException;
public class ExceptionForQuiz<T extends Exception> {private void pleaseThrow(final Exception t) throws T {throw (T) t;}public static void main(final String[] args) {try {new ExceptionForQuiz<RuntimeException>().pleaseThrow(new SQLException());}catch( final SQLException ex){System.out.println("Jerry print");ex.printStackTrace();}}
}

What result this program will generate?
Let’s analyze it step by step.

(1) The class ExceptionForQuiz uses a generic typing syntax extends to declare a bound that T only accepts Exception and its sub classes.
As a result in my main method code the creation of new ExceptionForQuiz via the below code is legal since according to JDK source code, RuntimeException is subclass of Exception.

new ExceptionForQuiz()

Also keep in mind that RuntimeException is a kind of Unchecked exception ( do not need to be declared in method where it might be raised ), which will be compared with ABAP exception later.

(2) According to Java Document, the type parameter in generic type declaration will be replaced by its bound during compile, in my exception RuntimeException will be replaced by Exception. This is called Type Erasure.
As a result, let’s forget about the try – catch for the moment.
This is original code:

This is the code decompiled from ExceptionForQuiz.class:

You can observe the fact of type erasure clearly.
You can also check the byte code by command javap, where the RuntimeException is erased.

(3) Now let’s see the result of this quiz.
The correct answer is: this program cannot pass compile! Compiler considers that SQLException could never have possibility to be raised from within TRY block.

Unfortunately, this is what I would like to do: raise SQLException via method pleaseThrow and catch it in catch block.

How to achieve my requirement?

Just change one line as highlighted below. Instead of catching SQLException, I now catch RuntimeException in order to pacify the compiler.

Now there is no compilation error any more but when executing it, I find the raised SQLException still cannot be caught as I expect. My println is not executed at all.

I have to catch the Exception, the super class of all other exception instead ( like CX_ROOT in ABAP ), which is not a good practice in exception handling area.

Handleable and Unhandleable Exception in ABAP

You can find both definition in ABAP help.

Let’s now do the similar exercise as we did previous in Java. Create a method with below signature.

Now make the first test:

DATA(lo_test) = NEW zcl_exception_test( ).DATA: lo_exception TYPE REF TO cx_atd_exception.CREATE OBJECT lo_exception.WRITE:/ 'First test' COLOR COL_NEGATIVE.TRY.lo_test->please_throw( lo_exception ).CATCH cx_atd_exception INTO DATA(exception1).WRITE:/ 'Jerry: ' , exception1->get_text( ).ENDTRY.

It works as expected, the raised exception is caught.

Now the second test:

  DATA: lo_exception2 TYPE REF TO cx_sql_exception.CREATE OBJECT lo_exception2.WRITE:/ 'Second test' COLOR COL_NEGATIVE.TRY.lo_test->please_throw( lo_exception2 ).CATCH cx_sql_exception INTO DATA(exception).WRITE:/ 'In catch sql exception:' , exception->get_text( ).ENDTRY.

The code is exactly the same as the first test, except that the exception is changed from CX_ATD_EXCEPTION to CX_SQL_EXCEPTION.
And result for the second test, 囧 …

In order to make this CX_SQL_EXCEPTION caught-able, I have to write the same dirty code as we did in Java example:

Although this time it works, but what is the reason of the different behaviors of these two examples?

The error message and short dump description have already given us a hint.
CX_ATD_EXCEPTION’s super class: CX_NO_CHECK. As its description says, it is not necessary to manually declare it in method signature using RAISING keyword.

And CX_SQL_EXCEPTION’s super class: CX_STATIC_CHECK

As a result now we have another solution:
Create another version of PLEASE_THROW method with RAISING keyword:


Use this new version and now CX_SQL_EXCEPTION could be caught:

  WRITE:/ 'Third test' COLOR COL_NEGATIVE.TRY.lo_test->please_throw2( lo_exception2 ).CATCH cx_sql_exception INTO DATA(exception3).WRITE:/ 'In catch sql exception:' , exception3->get_text( ).ENDTRY.

Further reading

I have written a series of blogs which compare the language feature among ABAP, JavaScript and Java. You can find a list of them below:

  • Lazy Loading, Singleton and Bridge design pattern in JavaScript and in ABAP
  • Functional programming – Simulate Curry in ABAP
  • Functional Programming – Try Reduce in JavaScript and in ABAP
  • Simulate Mockito in ABAP
  • A simulation of Java Spring dependency injection annotation @Inject in ABAP
  • Singleton bypass – ABAP and Java
  • Weak reference in ABAP and Java
  • Java byte code and ABAP Load
  • How to write a “correct” program rejected by compiler: Exception handling in Java and in ABAP
  • An small example to learn Garbage collection in Java and in ABAP
  • String Template in ABAP, ES6, Angular and React
  • Try to access static private attribute via ABAP RTTI and Java Reflection
  • Local class in ABAP, Java and JavaScript
  • Integer in ABAP, Java and JavaScript

要获取更多Jerry的原创文章,请关注公众号"汪子熙":

Java和SAP ABAP的异常处理相关推荐

  1. 浅谈Java和SAP ABAP的静态代理和动态代理,以及ABAP面向切面编程的尝试

    文章目录 Java的静态代理 静态代理的优缺点 ABAP的静态代理 Spring AOP的动态代理 JDK动态代理的优缺点 CGLIB动态代理的优缺点 ABAP CGLIB的模拟实现 ABAP Pre ...

  2. Jerry文章《浅谈Java和SAP ABAP的静态代理和动态代理,以及ABAP面向切面编程的尝试一文的源代码》

    原文链接 导航目录 工具类ZCL_ABAP_DYNAMIC_PROXY_FACTORY 工具类zcl_abap_cglib_tool Jerry Wang的ABAP专题文章 工具类ZCL_ABAP_D ...

  3. SAP ABAP Development Tool 提高开发效率的十个小技巧

    这是 Jerry 2021 年的第 46 篇文章,也是汪子熙公众号总共第 323 篇原创文章. Jerry 已经有很长一阵子没有打开工作电脑上的 SAP ABAP Development Tool 了 ...

  4. SAP ABAP一组关键字 IS BOUND, IS NOT INITIAL和IS ASSIGNED的用法辨析

    ABAP里的IS BOUND, IS NOT INITIAL和IS ASSIGNED这组关键字,如果平时不留心,很容易理解地似是而非.今天我们就来说一说它们的区别. 先把SAP帮助文档抄过来: IS ...

  5. SAP ABAP Netweaver服务器的标准登录方式讲解

    最近Jerry把这个公众号之前发布的总共230篇文章按照类别整理了一系列合集出来,比如所有的ABAP文章,放在了这个合集里:汪子熙的ABAP合集. 本文继续介绍ABAP里的一个知识点:ABAP Net ...

  6. 什么是 SAP ABAP? 类型、ABAP 完整形式和含义

    转载地址:https://www.guru99.com/what-is-abap.html ABAP 是一种由 SAP 创建的高级编程语言,可帮助大型企业定制 SAP ERP. ABAP 可以帮助定制 ...

  7. Java的Covariance设计原理和SAP ABAP的模拟实现

    I am the trainer of one standard course "Programming Language Concept" within SAP and ther ...

  8. SAP ABAP ADBC和Java JDBC的使用比较

    Horst Keller has already introduced ADBC in his blog ABAP Geek 15 – ADBC long times ago. And recentl ...

  9. SAP ABAP和Java里的弱引用(WeakReference)和软引用(SoftReference)

    Jerry前一篇文章 SAP ABAP一组关键字 IS BOUND, IS NOT INITIAL和IS ASSIGNED的用法辨析 介绍了在ABAP里判断引用变量是否包含了一个有效引用的关键字:IS ...

最新文章

  1. css3动画参数解释
  2. mysql数据导出权限问题
  3. CAN总线数据帧/标准帧/扩展帧/远程帧/错误帧的组成格式对比
  4. java不同进程的相互唤醒_Java线程生命周期与状态切换
  5. 【计算机系统设计】重点 · 学习笔记(0)
  6. python 批量查询网页导出结果_python批量查询网页的HTTP状态码
  7. mysql基本命令大全_Django 学习笔记之 如何设置和操作 mysql 数据库
  8. Java中如何循环删除一个集合(如List)中的多个元素
  9. mac mysql降版本_Mac卸載mysql並安裝mysql升級到8.0.13版本
  10. 【监听文件 多线程】使用java--WatchService监听文件 开启多线程copy文件
  11. 硬盘出错,导致文件坏了
  12. 服务器系统2012u盘安装教程,windows2012 u盘安装教程
  13. 【Pytorch官方教程】从零开始自己搭建RNN2 - 字母级RNN的生成任务
  14. MATLAB实现LSBR并采用卡方分析进行分析
  15. 怎么看台式计算机是几位的,怎么看电脑是32位还是64位,教您如何查看自己的电脑是32位的还是64位...
  16. mac 系统怎么更改 pip 源
  17. Kafka SASL/PLAIN 环境构建(Docker版)
  18. PreTranslateMessage()
  19. [HNOI2004]打鼹鼠 简单DP
  20. 《点云学习》----BD-Rate 和 BD-PSNR:计算和解释

热门文章

  1. 四中方式实现单例模式
  2. Oracle语句连接查询
  3. android - 常用知识点以及代码片段(不断更新)
  4. JavaSE学习之IO流使用技巧
  5. 从零写一个编译器(二):语法分析之前置知识
  6. 【11.5校内测试】【倒计时5天】【DP】【二分+贪心check】【推式子化简+线段树】...
  7. JavaScript If…Else 语句
  8. 美图笔试算法题(两个人拿石头判断输赢)
  9. Oracle之不可见索引
  10. Java开源权限管理中间件