views:

161

answers:

6

I'm trying to throw an exception in my code like this:

throw RuntimeException(msg);

But when I build in NetBeans I get this error:

C:\....java:50: cannot find symbol
symbol  : method RuntimeException(java.lang.String)
location: class ...
        throw RuntimeException(msg);
1 error

Do I need to import something? Am I misspelling it? I'm sure I must be doing something dumb :-(

+10  A: 

throw new RuntimeException(msg)

You need the new in there. It's creating an instance and throwing it, not calling a method.

j flemm
+2  A: 
throw new RuntimeException(msg); // notice the "new" keyword
naikus
+16  A: 

An Exception is an Object like any other in Java. You need to use the new keyword to create a new Exception before you can throw it.

throw new RuntimeException();

Optionally you could also do the following:

RuntimeException e = new RuntimeException();
throw e;

Both code snippets are equivalent.

Link to the tutorials for completeness.

jjnguy
+2  A: 

You need to create the instance of the RuntimeException, using new the same way you would to create an instance of most other classes:

throw new RuntimeException(msg);
RHSeeger
+2  A: 

you will have to instantiate it before you throw it

throw new RuntimeException(arg0) 

PS: Intrestingly enough the Netbeans IDE should have already pointed out that compile time error

Sudhakar
Why the downvotes? +1 to counter them.
Coronatus
+7  A: 

As everyone else has said, instantiate the object before throwing it.

Just wanted to add one bit; it's incredibly uncommon to throw a RuntimeException. It would be normal for code in the API to throw a subclass of this, but normally, application code would throw Exception, or something that extends Exception but not RuntimeException.

And in retrospect, I missed adding the reason why you use Exception instead of RuntimeException; @Jay, in the comment below, added in the useful bit. RuntimeException isn't a checked exception;

  • The method signature doesn't have to declare that a RuntimeException may be thrown.
  • Callers of that method aren't required to catch the exception, or acknowlege it in any way.
  • Developers who try to later use your code won't anticipate this problem unless they look carefully, and it will increase the maintenance burden of the code.
Dean J
Ditto. In general, you should create your own exceptions to throw, and inherit them from Exception.RuntimeException should be used with extreme care as it bypasses the normal guarantees in Java that a function must declare all the exceptions that it can throw.
Jay
One common case for doing this is if you're implementing an existing interface that doesn't declare any checked exceptions. For instance, writing a network-backed cache it makes sense to implement `Map` - but if you get an `IOException` when processing a `put()` or `get()`, you're going to have to throw some kind of RuntimeException. (It should be a custom class thereof, but it will have to be an unchecked exception).
Andrzej Doyle