How to throw RuntimeException ("cannot find symbol")

Greg picture Greg · Aug 4, 2010 · Viewed 144.9k times · Source

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

Answer

j flemm picture j flemm · Aug 4, 2010

throw new RuntimeException(msg);

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