Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>There're a few ways to go about this. The approach you take depends upon your requirements. If you merely want to print an error message, you can check your conditions and use System.out or System.err to emit your message:</p> <pre><code>if (conditionsNotMet) System.err.println("Error: Condition X is not met"); </code></pre> <p>Take a look at the javadoc for System.out and System.err (http://download.oracle.com/javase/1,5.0/docs/api/java/lang/System.html) -- They're both output streams, corresponding to stdout and stderr. stderr is typically used for emitting error and log messages, but how each of these outputs is used is up to you, the developer.</p> <p>If you wanted to abort processing, you might consider using an exception instead:</p> <pre><code>if (conditionsNotMet) throw new Exception("Condition X is not met"); </code></pre> <p>Consider that exceptions can be a burden on developers. Checked exceptions (http://www.javapractices.com/topic/TopicAction.do?Id=129) must be declared to be thrown or caught. In this case, perhaps that's what you want. </p> <p>It's bad form to throw a java.lang.Exception -- seriously consider an exception that is more suited (such as IllegalArgumentExeception, in this case) or implement your own. IllegalArgumentException is <em>not</em> a checked exception which might be good in your case. As I mentioned, checked exceptions must be declared as thrown or caught in a try/catch block. Seeing as IllegalArgumentException is not checked, you won't be forcing clients of your method to catch it. </p> <p>Often it's still a good idea to document when a method throws an unchecked exception, though. This informs other developers using your code the common conditions under which an exception is thrown allowing them to make a decision about catching or ignoring the exception.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload