35

All the googling I've done seems focused on "catching" errors. I want to be able to raise my own if certain conditions are met. I tried using the Error() class and its subclasses but Eclipse doesn't recognize them.

This is what I want to do:

if(some_condition) {
    foobar();
}
else {
    // raise an error
}

Stupid question, I know, but I've done my googling and I figure someone out there would be able to help me.

Thanks in advance!


Thanks everyone! If you are reading this in the future, here's the skinny:

  1. Errors in Java refer to problems that you should NOT try to catch

  2. Exceptions refer to errors that you may want to catch.

Here's my "fixed" code:

if(some_condition) {
    foobar();
}
else {
    throw new RuntimeError("Bad.");
}

I used RuntimeError() because, as one answer pointed out, I don't have to declare that I'm throwing an error beforehand, and since I'm relying on a condition, that's very useful.

Thanks all!

Darryl RN
  • 7,432
  • 4
  • 26
  • 46
Eli Dinkelspiel
  • 769
  • 1
  • 6
  • 15

5 Answers5

46

Here you go:

throw new java.lang.Error("this is very bad");

More idiomatic to throw a subclass of Exception. RuntimeException in particular is unchecked (e.g., methods don't need to declare that they might throw it). (Well, so is Error, but it's supposed to be reserved for unrecoverable things).

throw new java.lang.RuntimeException("this is not quite as bad");

Note: you don't actually have to construct them right at that moment. You can throw pre-constructed ones. But one thing nice about constructing them is they record the line of code they were on and the complete call-stack that happened at the time of construction, and so constructing a new one right when you throw it does inject it with very helpful diagnostic information.

G. Sylvie Davies
  • 5,049
  • 3
  • 21
  • 30
8

Try throwing an exception:

public void yourMethod() throws Exception {
    if (some_condition) {
        foobar();
    }
    else {
        throw new Exception("Something bad happened.");
    }
}
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
3

No need to feel dumb :-) You can create your own exception, e.g.,

public class UnknownUnitException extends Exception{
    private String message = null;
    public UnknownUnitException() {
        super();
    }
    public UnknownUnitException(String message) {
        super(message);
        this.message = message;
    }
    public UnknownUnitException(Throwable cause) {
        super(cause);
    }
    @Override
    public String toString() {
        return message;
    }
    @Override
    public String getMessage() {
        return message;
    }
}

I'd recommend Lesson: Exceptions at the Java Tutorials

Tamias
  • 173
  • 9
1

An error in Java is called Exception (See this).

if(some_condition){
   foobar();
} else {
   throw new Exception();
}

Formally, an error is any class that extends from Throwable.

The Throwable class is the superclass of all errors and exceptions in the Java language. Only objects that are instances of this class (or one of its subclasses) are thrown by the Java Virtual Machine or can be thrown by the Java throw statement. Similarly, only this class or one of its subclasses can be the argument type in a catch clause. For the purposes of compile-time checking of exceptions, Throwable and any subclass of Throwable that is not also a subclass of either RuntimeException or Error are regarded as checked exceptions.

...

https://docs.oracle.com/javase/7/docs/api/java/lang/Throwable.html

Community
  • 1
  • 1
Manuel Vieda
  • 296
  • 5
  • 12
1

Are you trying to raise a error in console only? Or are you trying to show an error to the user?

In which case you can use a JOptionPane to create a error dialog box.

  • @eli_dink Take a look at this for Error vs Exception: https://stackoverflow.com/questions/5813614/what-is-difference-between-errors-and-exceptions – Shivam Sinha Jan 22 '16 at 03:33