[java] How to throw RuntimeException ("cannot find symbol")

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

This question is related to java exception-handling runtimeexception

The answer is


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);

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.


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


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.


using new keyword we always create a instance (new object) and throwing 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 crate 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);

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.

throw new RuntimeException(msg); // notice the "new" keyword

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


Examples related to java

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How much should a function trust another function How to implement a simple scenario the OO way Two constructors How do I get some variable from another class in Java? this in equals method How to split a string in two and store it in a field How to do perspective fixing? String index out of range: 4 My eclipse won't open, i download the bundle pack it keeps saying error log

Examples related to exception-handling

Catching FULL exception message Spring Resttemplate exception handling How to get exception message in Python properly Spring Boot REST service exception handling java.net.BindException: Address already in use: JVM_Bind <null>:80 Python FileNotFound The process cannot access the file because it is being used by another process (File is created but contains nothing) Java 8: Lambda-Streams, Filter by Method with Exception Laravel view not found exception How to efficiently use try...catch blocks in PHP

Examples related to runtimeexception

Error:Execution failed for task ':app:transformClassesWithDexForDebug' in android studio SEVERE: Unable to create initial connections of pool - tomcat 7 with context.xml file Why doesn't catching Exception catch RuntimeException? ActionBarCompat: java.lang.IllegalStateException: You need to use a Theme.AppCompat NameError: global name 'xrange' is not defined in Python 3 Unable instantiate android.gms.maps.MapFragment Error: Registry key 'Software\JavaSoft\Java Runtime Environment'\CurrentVersion'? Android 'Unable to add window -- token null is not for an application' exception Understanding checked vs unchecked exceptions in Java Can't create handler inside thread which has not called Looper.prepare()