DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Related

  • How To Build Translate Solutions With Google Cloud Translate AI
  • Lost in Translation: Gaps of GPT-3.5 in South Asian and Middle Eastern Languages
  • PostgresML: Extension That Turns PostgreSQL Into a Platform for AI Apps
  • Transforming Translation: The Power of Context in NLP

Trending

  • Build a Simple REST API Using Python Flask and SQLite (With Tests)
  • Efficient API Communication With Spring WebClient
  • Introducing Graph Concepts in Java With Eclipse JNoSQL
  • The Evolution of Scalable and Resilient Container Infrastructure

Exception Translation with ET

Learn how to do exception translation with AspectJ using the ET (exception translation) library and it's lighter Java 8 approach.

By 
Michael Scharhag user avatar
Michael Scharhag
·
Jun. 08, 15 · Interview
Likes (3)
Comment
Save
Tweet
Share
10.2K Views

Join the DZone community and get the full member experience.

Join For Free

Some time ago I wrote a small blog post about exception translation with AspectJ. In this blog post we will see how to accomplish the same using ET and its lighter Java 8 approach.

Motivation
Exception translation (or exception conversion) is the process of converting one type of exception into another.
The Java code to translate an exception is quite simple and I think every Java developer writes something like this from time to time:

try {
  // code that can throw FooException
} catch(FooException e) {
  // convert FooException to BarException
  throw new BarException(e);
}

Exception translation is typically applied if exceptions from third party libraries do not fit into your application. Reasons for this might be:

  • Exceptions thrown by a library are too low level and/or you do not want to leak implementation details into other parts of your application. For example, you want to use a more genericDataAccessException instead of a lower level SQLException.
  • A library is using checked exception while you prefer using only runtime exception in your application.


Exception Translation with ET

ET is a small and simple library for exception translation. To get started with ET, you just need to add the following dependency to your code:

<dependency>
  <groupId>com.mscharhag</groupId>
  <artifactId>et</artifactId>
  <version>0.2.0</version>
</dependency>

ET makes use of Java 8 features, so do not forget to set your compiler Level to Java 8.

We start with configuring an ExceptionTranslator instance:

ExceptionTranslator et = ET.newConfiguration()
    .translate(IOException.class).to(MyRuntimeException.class)        
    .translate(FooException.class, BarException.class).to(BazException.class)
    .done()

Here we create an ExceptionTranslator that converts IOException, FooException andBarException. IOException will be translated to MyRuntimeException while FooException andBarException are translated to BazException.

Please note that ET requires the translation target exceptions (here MyRuntimeException andBazException) to be RuntimeExceptions.
ExceptionTranslator instances are thread safe and immutable. It is safe to configure anExceptionTranslator once and then make it globally available.

Now we can use our new ExceptionTranslator to wrap the code that can throw exceptions we want to convert.

et.withTranslation(() -> {
  // can throw IOException, FooException and/or BarException
  myObject.dangerOperation(); 
});

f now an IOException is thrown by dangerOperation()et will catch it. et then throws a newMyRuntimeException from the caught IOException. The original IOException is stored in the causefield of MyRuntimeException.

To return a value from a translation block withReturningTranslation() can be used:

MyResultClass data = et.withReturningTranslation(() -> {
  ...
  return myObject.dangerOperation(); 
});

Summary
ET is a small library that might be useful to you, if you have to do a lot of exception conversion in your code. After configuring your conversion rules once, exceptions can be converted by simply wrapping the code in a lambda expression.

Have a look at the full ET documentation on GitHub. 

Translation

Published at DZone with permission of Michael Scharhag, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • How To Build Translate Solutions With Google Cloud Translate AI
  • Lost in Translation: Gaps of GPT-3.5 in South Asian and Middle Eastern Languages
  • PostgresML: Extension That Turns PostgreSQL Into a Platform for AI Apps
  • Transforming Translation: The Power of Context in NLP

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!