Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8366507
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T12:56:14+00:00 2026-06-09T12:56:14+00:00

Too often one sees the following pattern in enterprising Java programs public Something myMethod()

  • 0

Too often one sees the following pattern in enterprising Java programs

public Something myMethod() throws MyException {
    try {
        // may throw checked DbException:
        Connection c = openDataBase("connectionstring");
        // ... other code throwing more checked exceptions ...
    } catch(DbException e) {
        throw new MyException(e);
    }
    return something;
}

…or any other mechanism to “cast” one checked exception type that is not declared in the method header to the one that is. Very often this “try-catch-cast”-block must go into every method of such a class.

I wonder, how to implement an annotation, that catches and converts exceptions?

The using code should then look like this:

@ConvertException(MyException, DbException)
public Something myMethod() throws MyException {
    // may throw checked DbException:
    Connection c = openDataBase("connectionstring");
    // ...
    return something;
}

Of course, Maybe one has to write MyException.class or "MyException" instead. It should also be possible to chain these annotations, or list multiple exceptions to convert.

My guess is that the annotation would introduce a wrapper function with the catching code block that would call the original function. The annotation would then only have compile-time-retension (and not run-time-retension).

I don’t advocate that its wise to do this, it probably isn’t because these annotations change the program semantics. It may well be an academical question to “just learn”…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T12:56:15+00:00Added an answer on June 9, 2026 at 12:56 pm

    Annotations don’t do anything at all by themselves. You need a tool that evaluates them and does some code changing according to them.

    In your case, AspectJ seems to be the best match.

    My advice would be to read AspectJ in Action (2nd ed) by Ramnivas Laddad.

    As you can see from it’s Table of Content, it contains a chapter about exception softening, which is almost exactly what you want.

    And since you tagged this question dependency-injection, assuming you use Spring, here is Spring’s own Exception translation mechanism

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am finding myself doing the following a bit too often: attr = getattr(obj,
I don't see this problem too often but I've got a .cshtml that uses
I've learned the basic/intermediate levels of VIM (it's too vast to list). I often
All too often I've run into a situation in which a view in my
I often read that one purpose of a VIEW is security: to allow some
Here's a situation that comes up quite often that never sits too easy with
So, it seems like those one letter application names are becoming way too popular,
All too often I want a WPF slider that behaves like the System.Windows.Forms.TrackBar of
I tend to take the academic approach all too often and adhere to strict
This question is motivated by something I've lately started to see a bit too

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.