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 8999703
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:12:54+00:00 2026-06-16T00:12:54+00:00

I have a design level doubt regarding creating APIs in Java. Suppose I have

  • 0

I have a design level doubt regarding creating APIs in Java. Suppose I have a class as follows :-

class Test
{
  public final static String DEFAULT_ENCODING = "utf-8";

  public byte[] encodeIt(String input)
  {
    try {
      return input.getBytes(DEFAULT_ENCODING);
    } catch(UnsupportedEncodingException e) {
      // do something
    }
  }
}

I know that the UnsupportedEncodingException would never arise as I’m using a static string as the input to toBytes. It doesn’t make sense to have encodeIt do a throws UnsupportedEncodingException because I dont wish the API users to expect and catch that error either.

In such cases, is the best practice to have an empty catch block?

  • 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-16T00:12:55+00:00Added an answer on June 16, 2026 at 12:12 am

    It is a bad idea to have empty catch blocks. Even though your reasoning seems correct this design will at some stage cause you endless debugging and searching once exceptions do start happening and your code is swallowing them. I would wrap your exception in a RuntimeException here and throw that. Like so:

    public encodeIt(String input)
      {
        try {
          return input.getBytes(DEFAULT_ENCODING);
        catch(UnsupportedEncodingException e) {
          throw new RuntimeException(e);
        }
      }
    

    This way your exception will not stay hidden might it occur nor do your API users have to cater for it.

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

Sidebar

Related Questions

In subsystem design, I sometimes see software designs that have one high-level class that
What could have been the design decision behind restricting a class to either public
I have a design problem: Question class: have a question and answer (String) TextQuestion
I have this design: public interface IFactory<T> { T Create(); T CreateWithSensibleDefaults(); } public
I have built a test application in Android with minimum API level as 8
I design new class, which contains the same function boolean isCellEmpty() at each level
I have an object of class Level that has several subsystems (child objects) it
I have to design a GUI using Qt. I would like to draw multiple
I have a design problem: I have two threads, a heartbeat/control thread and a
I have a design question. We need to put some default, system defined content

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.