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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T22:12:12+00:00 2026-05-25T22:12:12+00:00

I am writing a small library, and in which I need to access several

  • 0

I am writing a small library, and in which I need to access several different type of files. While the access method itself is different for each kind of file format, they seem to have a lot in common, and I put an interface in the class hierarchy, in which I wrote a method that should connect to the data source.

However, since the data source might be protected by password and/or user permission, sometimes it need authentication to retrieve the data. My questions are:

  1. It is a good idea to throw an exception when authentication is required?

    Since I want to expose the implementation as little as possible, I only want to tell the user what happened. But authentication might need many different things (username, password, etc.), so could I pack them into one exception and throw it out? Or, maybe there is a better way without resorting to exceptions, since “Authentication required” is not really the exceptional behavior that exception usually used to handle.

  2. What exception to throw when authentication is required?

    Now suppose I decided to use exception to handle this. Which exception should I throw? The several AuthenticationExceptions shipped with Java API does not seem to fit this requirement since they all seem to be very case specific, e.g., used in the naming service. I am not sure if SecurityException is the way to go, but if this is improper, I still really do not want to throw my own exception, since that will impede other people to understand my code and what is going on behind the API.

Thanks for any input! This is somewhat lengthy or maybe too verbose, so any edits that would improve the question is extremely welcomed.

  • 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-05-25T22:12:12+00:00Added an answer on May 25, 2026 at 10:12 pm

    Since it’s your own API, you might create your own Exception to go with it, which can carry the details… There’s no requirement or benefit to using the Java exception that “sounds closest to” your exception.

    I personally find that peppering my code with try/catch blocks is… tedious and unsightly. So I try to make API’s that don’t require it.

    In your case, maybe you could provide queries so your API clients could preflight the actions, and their usage might look something like:

    Thing t = new Thing(...);
    if(t.needsAuth())
    {
      boolean ok = t.doPassword("abc123");
      if(!ok)
        log("wrong password");
    }
    boolean did= t.doIt();
    if(!did)
      log("sorry: " + t.getProblem());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing a small library with which enums in C++ should get easier.
I'm writing a small services which will store files on disk and later, on
Purpose I'm writing a small library for which portability is the biggest concern. It
I'm writing a small tool in C# which will need to send and receive
I'm writing a small benchmarking library in C which is used to benchmark single
all. I'm pretty new to C++, and I'm writing a small library (mostly for
I've been writing some small maintenance/viewer tools that each connect to a SQLServer (2005)
I am writing a small I/O library to assist with a larger (hobby) project.
I'm writing a small Ruby command-line application that uses fileutils from the standard library
I'm writing a C library with GMP and its arbitrary precision floats. I need

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.