Quick question about a java program

JohnCU

Banned
Dec 9, 2000
16,528
4
0
MemoryManager.java:166: unreported exception InsufficientMemory; must be caught or declared to be thrown
M.request(50);

I keep getting that message when I try to run my program. It's supposed to throw an Insufficient Memory exception when it requests a block of memory too big...

public int request(int n) throws InsufficientMemory

if (too big)
throw new InsufficientMemory();



public class InsufficientMemory extends Exception
{
public InsufficientMemory()
{
}

public InsufficientMemory(String message)
{
super(message);
}
}


whats wrong with that?
 

Kilrsat

Golden Member
Jul 16, 2001
1,072
0
0
Replace:
M.request(50);

With:
try {
M.request(50);
} catch (InsufficientMemory im) {
System.err.println("Yo foo, needs to buy mo' shizzle");
im.printStackTrace();
}

The compiler is telling you that "M.request(50);" could cause an InsufficientMemory exception. Because of this, you need to handle that, either by placing "M.request(50);" in a try/catch block, or by declaring the method that calls "M.request(50);" as throwing an InsufficientMemory exception.