Java和SAP ABAP的异常处理-阿里云开发者社区

开发者社区> c位出道> 正文

Java和SAP ABAP的异常处理

简介: Java和SAP ABAP的异常处理
+关注继续查看

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.


image.png

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.


image.png(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:


image.pngThis is the code decompiled from ExceptionForQuiz.class:

image.pngYou can observe the fact of type erasure clearly.

You can also check the byte code by command javap, where the RuntimeException is erased.image.png(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.


image.pngHow 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.


image.pngI 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.



image.png

Handleable and Unhandleable Exception in ABAP

You can find both definition in ABAP help.image.pngLet’s now do the similar exercise as we did previous in Java. Create a method with below signature.image.pngNow 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.image.pngNow 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, 囧 …



image.pngIn order to make this CX_SQL_EXCEPTION caught-able, I have to write the same dirty code as we did in Java example:image.pngAlthough this time it works, but what is the reason of the different behaviors of these two examples?image.pngThe 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

image.png


And CX_SQL_EXCEPTION’s super class: CX_STATIC_CHECK

image.pngAs a result now we have another solution:

Create another version of PLEASE_THROW method with RAISING keyword:image.pngimage.pngUse 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


版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

相关文章
阿里云服务器怎么设置密码?怎么停机?怎么重启服务器?
如果在创建实例时没有设置密码,或者密码丢失,您可以在控制台上重新设置实例的登录密码。本文仅描述如何在 ECS 管理控制台上修改实例登录密码。
10088 0
【Java入门提高篇】Day16 Java异常处理(上)
  当当当当当当,各位看官,好久不见,甚是想念。   今天我们来聊聊Java里的一个小妖精,那就是异常。 什么是异常?什么是异常处理?   异常嘛,顾名思义就是不正常,(逃),是Java程序运行时,发生的预料之外的事情,它阻止了程序按照程序员的预期正常执行。
940 0
学习Java的异常处理 -- 2015-05-16记录
写这篇文章的目的是总结这几天遇到关于Java的Exception的处理,总是遇到了问题才发现,原来自己的基础很薄弱,平时要多花点时间去深入学习基础的东西。
608 0
JAVA异常处理的几种陋习
你觉得自己是一个Java专家吗?是否肯定自己已经全面掌握了Java的异常处理机制?在下面这段代码中,你能够迅速找出异常处理的六个问题吗? 1 OutputStreamWriter out = .
506 0
怎么设置阿里云服务器安全组?阿里云安全组规则详细解说
阿里云服务器安全组设置规则分享,阿里云服务器安全组如何放行端口设置教程
8477 0
阿里云服务器端口号设置
阿里云服务器初级使用者可能面临的问题之一. 使用tomcat或者其他服务器软件设置端口号后,比如 一些不是默认的, mysql的 3306, mssql的1433,有时候打不开网页, 原因是没有在ecs安全组去设置这个端口号. 解决: 点击ecs下网络和安全下的安全组 在弹出的安全组中,如果没有就新建安全组,然后点击配置规则 最后如上图点击添加...或快速创建.   have fun!  将编程看作是一门艺术,而不单单是个技术。
10883 0
阿里云服务器如何登录?阿里云服务器的三种登录方法
购买阿里云ECS云服务器后如何登录?场景不同,阿里云优惠总结大概有三种登录方式: 登录到ECS云服务器控制台 在ECS云服务器控制台用户可以更改密码、更换系.
13891 0
+关注
2316
文章
0
问答
来源圈子
更多
+ 订阅
文章排行榜
最热
最新
相关电子书
更多
《2021云上架构与运维峰会演讲合集》
立即下载
《零基础CSS入门教程》
立即下载
《零基础HTML入门教程》
立即下载