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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T19:55:15+00:00 2026-06-06T19:55:15+00:00

I have a static method that returns a String, but in the event that

  • 0

I have a static method that returns a String, but in the event that the string that is passed in does not match one of several words, I want to throw an exception. The code below is just a sample of what I am trying to do, but I keep getting “non static variable this cannot be referenced from a static context” message on the line where I throw the exception. Basically, the return value from getMsg has to be valid, or the program cannot proceed, so I need a way to catch this.

public static String getMsg(String input) throws UnknownInputException{
      if (input.equals("A")){
           return "key for A";
      }
      throw new UnknownInputException("Some Message");
      return "unknownInput";
  • 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-06T19:55:18+00:00Added an answer on June 6, 2026 at 7:55 pm

    The problem is caused by the fact, that UnknownInputException is probably a nested class, and if you instantiate it with the new operator, as a nested class, it should have access to a “parent” object – which doesn’t exist since the class was instantiated in a static context. For more information about this, see Static method returning inner class.

    A possible solution would be to declare UnknownInputException as static like this:

    private static class UnknownInputException extends Exception { ... }
    

    Of course, you won’t be able to access any instance (non-static) methods and/or fields from this class, but that might not be an issue in your case (especially in case of an Exception class).

    Also, returning value after the throw line is unnecessary, as execution will never reach that line.

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

Sidebar

Related Questions

I have async methods that returns an objects public static IEnumerable<Users.User> GetUsers(IEnumerable<string> uids, Field
I have a static method that takes a parameter and returns a class. the
Suppose I have a static method of my class that returns an object of
I understand that static method inheritance is not supported in C#. I have also
I have a method that looks like this: public static String escape(String text) {
I have written code to perform click event on JMenuItem but that is not
I have the following static method that prints the data imported from a 40.000
I have a static method in my code that I would like somehow to
I have a C# database layer that with static read method that is called
Welcome! I have a recursive public static method named less that takes a tree

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.