Petar pointed me to this example code (from msdn)
void WriteLog()
{
if (!this.logFile.CanWrite)
{
throw new System.InvalidOperationException("Logfile cannot be read-only");
}
// Else write data to the log and return.
}
So in this context you could use an IllegalStateException, although it says:
Thrown when an action is attempted at a time when the virtual machine is not in the correct state.
And an illegal VM state is definitly not the issue in the above reference example. Here, the problem is that the object is invalid, because it references a read-only logfile.
My own advice: just define a custom exception like
package com.pany.project;
public class InvalidOperationException extends RuntimeException {
// add constructors with call to super as needed
}
To me, that's much easier then trying to find the best fitting Exception from the java.lang
package.
Error
subclasses, although in this specific case, I think it would be classed anException
instead (probably aRuntimeException
). – Jugal