Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

My own exception in java - cannot find symbol

Tags:

java

exception

I'm trying to create my own exceptions, but encountered a problem...

class MyException extends Exception {
    MyException (String w) { super ( w ); }
};


public class RozkladLiczby{

    private int[] Array;

    RozkladLiczby(int n) throws MyException { 

        try{
            Array = new int[n+2];
        }
        catch(OutOfMemoryException ex){
            throw new MyException ("Not enough memory" );
        }
    }
}

Compiler says it can't find symbol in catch.

RozkladLiczby.java:16: error: cannot find symbol
    catch(OutOfMemoryException ex){
          ^


 symbol:   class OutOfMemoryException
  location: class RozkladLiczby
1 error
like image 894
wojteo Avatar asked Aug 09 '26 10:08

wojteo


1 Answers

That's because there is no OutOfMemoryException in Java; there is OutOfMemoryError. And if there's no memory left, then what can you do? There's no memory to create your new exception. That's why it's an Error and not an Exception.

like image 185
rgettman Avatar answered Aug 11 '26 23:08

rgettman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!