Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can static methods that return a value throw an exception?

Tags:

java

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";
like image 795
user1154644 Avatar asked Jul 03 '12 13:07

user1154644


People also ask

Can static method throw exception?

A static block can throw only a RunTimeException, or there should be a try and catch block to catch a checked exception. A static block occurs when a class is loaded by a class loader.

Can static methods have return values?

A static method can also provide an array as a return value.

Which Cannot be used in static method?

A static method cannot access a class's instance variables and instance methods, because a static method can be called even when no objects of the class have been instantiated. For the same reason, the this reference cannot be used in a static method.

What are the restrictions of static method?

Restrictions on static blocks and static methodsYou cannot access a non-static member (method or, variable) from a static context. This and super cannot be used in static context. The static method can access only static type data (static type instance variable). You cannot override a static method.


2 Answers

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.

like image 138
dnet Avatar answered Sep 20 '22 21:09

dnet


The variable this is not noted in the given example code, so it can not cause an error.

The code return "unknownInput"; is redundant since never executed.

There must be another static method in which this is used, that causes the error.

like image 41
Michael Besteck Avatar answered Sep 22 '22 21:09

Michael Besteck