46

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 :-(

Raedwald
  • 46,613
  • 43
  • 151
  • 237
Greg
  • 45,306
  • 89
  • 231
  • 297
  • 3
    Why would you want to throw a RuntimeException? – Jonathon Faust Aug 04 '10 at 13:58
  • 10
    @JonathonFaust - Because sometimes you neither want to handle the exception yourself, nor want to force your user to handle the exception. But if nobody handles it, the application should crash. In Python, nearly every exception is a runtime exception, and everyone loves it. If you know how the exception should be handled, you handle it, and if you don't, then you ignore it - either someone above you can handle it or you'll crash, and either scenario is fine. The only thing that wouldn't be fine is logging it and otherwise ignoring it. – ArtOfWarfare Sep 10 '15 at 14:34
  • Do not throw a runtime exception or create a subclass of RuntimeException simply because you don't want to be bothered with specifying the exceptions your methods can throw. http://journals.ecs.soton.ac.uk/java/tutorial/java/exceptions/runtime.html – Martin Spamer Nov 10 '15 at 11:08
  • 1
    It needs to add "new" keyword after throw. – Ripon Al Wasim Nov 13 '15 at 09:20

9 Answers9

139

throw new RuntimeException(msg);

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

slm
  • 15,396
  • 12
  • 109
  • 124
j flemm
  • 1,989
  • 1
  • 12
  • 6
  • 3
    @PedroD Because checked exceptions are a failed experiment? Or are you asking why someone would want to throw a *raw* `RuntimeException` instead of a reasonable subclass? – Dave Newton Feb 21 '16 at 19:48
  • No no, checked exceptions should be the only type of throwables you could use. They are part of the API you are creating/defining. Unchecked exceptions should be reserved for system errors which cannot/should not be recovered (like NullPointer or OutOfMemory). Eg. If I have a function that parses a number from a string, it should throw me a **checked** NumberFormatException, because it is expected that this function might fail for that reason and it is totally reasonable that I might catch and recover from that error, so it should be checked... However if I get a Null pointer... – PedroD Feb 21 '16 at 19:59
  • 1
    ...I is very very very likely a programming mistake (some unhandled fault condition) so the app should terminate with that exception, which is **unchecked** on purpose. Unchecked exceptions are to be expected to happen anywhere in the program and should only be recovered if we really know what are the implications of that recovery, hence why we must explicitly surround then with a try-catch and the compiler cannot tell where they are thrown. – PedroD Feb 21 '16 at 20:03
  • You also need to use runtime exceptions in case you've a static {} block where you're doing instantiation and want to throw an exception if that instantiation fails. Since static blocks do not have throw/throws keyword, RuntimeExceptions become suitable in this case. – Ankit Arora Sep 05 '18 at 11:37
38

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.

Michal
  • 3,218
  • 2
  • 25
  • 44
jjnguy
  • 136,852
  • 53
  • 295
  • 323
16

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
  • 39,360
  • 16
  • 67
  • 93
  • 1
    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 Aug 04 '10 at 16:46
  • 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 Aug 05 '10 at 13:51
5

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
  • 4,823
  • 2
  • 35
  • 42
4
throw new RuntimeException(msg); // notice the "new" keyword
naikus
  • 24,302
  • 4
  • 42
  • 43
3

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
  • 16,034
  • 7
  • 51
  • 41
2

Using the new keyword will always create an instance (new object) and throw it, not called a method

    throw new RuntimeException("Your Message");
    
    // You need the new in there. It's creating an instance and throwing it, not calling a method.
    
    int no = new Scanner().nextInt(); // we create an instance using new keyword and throwing it 

Using new keyword memory clean [because use and throw]:

    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
    
            //do your work here..
        }
    }, 1000);
Dmitriy Popov
  • 2,150
  • 3
  • 25
  • 34
Keshav Gera
  • 10,807
  • 1
  • 75
  • 53
1

Just for others: be sure it is new RuntimeException, not new RuntimeErrorException which needs error as an argument.

dillip
  • 1,782
  • 17
  • 16
1
throw new RuntimeException(msg);

unlike any other Exceptions I think RuntimeException is the only one that will not stall the program but it can still keep running and recover just print out a bunch of Exception lines? correct me if I am wrong.

SSpoke
  • 5,656
  • 10
  • 72
  • 124