Top exception Questions

List of Tags

Recently I ran into this error in my web application:

java.lang.OutOfMemoryError: PermGen space

It's a typical Hibernate/JPA + IceFaces/JSF application running on Tomcat 6 and JDK 1.6.

Apparently this can occur after redeploying an application a few times.

Answered By: Chris ( 262)

The solution was to add these flags to JVM command line when Tomcat is started:

-XX:+CMSClassUnloadingEnabled -XX:+CMSPermGenSweepingEnabled

You can do that by shutting down the tomcat service, then going into the Tomcat/bin directory and running tomcat6w.exe. Under the "Java" tab, add the arguments to the "Java Options" box. Click "OK" and then restart the service.

If you get an error the specified service does not exist as an installed service you should run:

tomecat6w //ES//servicename

where servicename is the name of the server as viewed in services.msc

Source: orx's comment on Eric's Agile Answers.

Max Charas

Is it possible to construct a snippet of code in Java that would make a hypothetical java.lang.ChuckNorrisException uncatchable?

Thoughts that came to mind are using for example interceptors or aspect-oriented programming.

Answered By: jtahlborn ( 198)

I haven't tried this, so I don't know if the JVM would restrict something like this, but maybe you could compile code which throws ChuckNorrisException, but at runtime provide a class definition of ChuckNorrisException which does not extend Throwable.


It doesn't work. It generates a verifier error:

Exception in thread "main" java.lang.VerifyError: (class: TestThrow, method: ma\
in signature: ([Ljava/lang/String;)V) Can only throw Throwable objects
Could not find the main class: TestThrow.  Program will exit.


Actually, you can get this to work if you disable the byte code verifier! (-Xverify:none)


For those following from home, here is the full script:

Create the following classes:

public class ChuckNorrisException
    extends RuntimeException // <- Comment out this line on second compilation
    public ChuckNorrisException() { }

public class TestVillain {
    public static void main(String[] args) {
        try {
            throw new ChuckNorrisException();
        catch(Throwable t) {
        finally {
            System.out.println("The end.");

Compile classes:

javac -cp .


java -cp . TestVillain
The end.

Comment out "extends RuntimeException" and recompile only :

javac -cp .


java -cp . TestVillain
Exception in thread "main" java.lang.VerifyError: (class: TestVillain, method: main signature: ([Ljava/lang/String;)V) Can only throw Throwable objects
Could not find the main class: TestVillain.  Program will exit.

Run without verification:

java -Xverify:none -cp . TestVillain
The end.
Exception in thread "main"
Michael Stum

It is discouraged to simply catch System.Exception, instead only the "known" Exceptions should be caught.

Now, this sometimes leads to unneccessary repetetive code, for example:

    WebId = new Guid(queryString["web"]);
catch (FormatException)
    WebId = Guid.Empty;
catch (OverflowException)
    WebId = Guid.Empty;

I wonder: Is there a way to catch both Exceptions and only call the WebId = Guid.Empty call once?

Edit: the given example is rather simple, as it's only a Guid. But imagine Code where you modify an object multiple times, and if one of the manipulations fail in an expected way, you want to "reset" the object. However, if there is an unexpected Exception, I still want to throw that higher.

About the Answer: Thanks everyone! For some reason, I had my mind set on a switch-case statement which does not support switching on GetType(). Now, there were two answers, one using "typeof" and one using "is". I first thought "typeof()" would be my Function because I thought "Hey, I only want to catch FormatException because that's the only thing I expect". But that's not how catch() works: catch also catches all derived exceptions. After thinking about it, this is really obvious: Otherwise, catch(Exception ex) would not work! So the correct answer is "is". Yay, learned two things with only one question \o/

Answered By: Joseph Daigle ( 363)

Catch System.Exception and switch on the types

catch (Exception ex)            
    if (ex is FormatException ||
        ex is OverflowException)
        WebId = Guid.Empty;

I have always been of the belief that if a method can throw an exception then it is reckless not to protect this call with a meaningful try block.

I just posted 'You should ALWAYS wrap calls that can throw in try, catch blocks.' to this question and was told that it was 'remarkably bad advice' - I'd like to understand why.


Answered By: Mitch Wheat ( 149)

A method should only catch an exception when it can handle it in some sensible way.

Otherwise, pass it on up, in the hope that a method higher up the call stack can make sense of it.

As others have noted, it is good practice to have an unhandled exception handler (with logging) at the highest level of the call stack to ensure that any fatal errors are logged.

devoured elysium

Is there anything like .net's NotImplementedException in java?