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

  • Home
  • SEARCH
  • 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 6761873
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:14:36+00:00 2026-05-26T14:14:36+00:00

I’ve got this method I’m working on (I think that’s the name) and it

  • 0

I’ve got this method I’m working on (I think that’s the name) and it essentially tries to match a part of a string and then return the proceeding part of the string – that part I’ve got, easy stuff. The method is of type String.

When my method fails to find the pattern in the string I want it to return an empty string. I also want to send something along with the empty string to go “hey, I didn’t find your key” but I want this to be optional.

This is essentially what I want the method to do:

public static String getKey(String key) throws KeyNotFoundException {
    if (key.equals("something")) {
        return "great";
    } else {
        throw new KeyNotFoundException();
        return "";
    }
}

But the problem with this code is that the return ""; is obviously unreachable due to throw new KeyNotFoundException();.

If I was going to call this method I’d have to use the try {} catch(KeyNotFoundException knf) {} block. It’s this block that I want to make optional however.

If I choose to be oblivious to the fact the key was not found (e.g. don’t use try catch) then I just want to be given the empty string on return.

But I also want to be able to surround it with a try-catch block if I need to know whether the key was found for my operation to complete correctly.

I think I’m going about this the wrong way but I can’t seem to figure out an alternative (new to Java), could anyone shred some light on this please?

  • 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-26T14:14:37+00:00Added an answer on May 26, 2026 at 2:14 pm

    The usual way to do this would be to write two methods, one which returns a default value, and one which throws an exception. You can have one of these call the other, to avoid duplication:

    public static String getKey(String key) throws KeyNotFoundException {
        String value = getOptionalKey(key);
        if (value.equals("")) throw new KeyNotFoundException(key);
        return value;
    }
    
    public static String getOptionalKey(String key) {
        if (key.equals("something")) {
            return "great";
        } else {
            return "";
        }
    }
    

    The caller can then choose which one to call, based on their needs:

    String value = getOptionalKey("uma"); // oblivious
    
    try {
        String value = getKey("uma"); // cognisant
    }
    catch (KeyNotFoundException e) {
        // panic
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a string that has curly quotes in it. I'd like to replace
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
For some reason, after submitting a string like this Jack’s Spindle from a text
i got an object with contents of html markup in it, for example: string
Does anyone know how can I replace this 2 symbol below from the string
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
I want to count how many characters a certain string has in PHP, but
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has

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.