Possible Duplicate:
What does assert do?
assert tests the programmer’s assumption during development without writing exception handlers for an exception This is what i got, when i was searching about the assert.
Apart from this, people also said, it is alternative of exception handling. Assertion will come into the picture when you don’t want to take the time to write the exception handling code. But, i didn’t get the working and uses. Anyone explain this example.
class AssertExample {
public static void main(String[] args) {
int x = 0;
assert (x > 0) ? "assertion failed" : "assertion passed";
System.out.println("finished");
} }
assertisn’t a way to handle exceptions, it’s a way to detect them. (Thus, the descriptions you’re finding seem a little off the mark to me.) It’s basically a way to say:How you handle that error is an entirely different concern. You might handle it right there in the function that detected it, you might handle it in the function that called it, you might handle it at the very top of the stack at the application level, etc.
Keep in mind that there’s a significant difference between catching an exception (which is a construct of the programming language being used) and meaningfully handling an exception (which is a logical construct independent of the language being used). Only catch exceptions where you can actually do something about them. Otherwise, let them bubble up the stack to other code that can do something about them. (But since
assertdoesn’t catch errors, it throws them, it should be used exactly where you’re trying to use it… the point at which an error can be detected, even if it can’t be handled.)Looking at your attempt to use
assert, it looks like you’re close. This isn’t really the correct usage:You’re treating the
assertas though it’s just a boolean. And then, using the? :operator, you’re keying off of that boolean to… well… not really do anything. Just return a string ("assertion failed"or"assertion passed") to a line of code that doesn’t do anything with that string.Close, but not quite.
The
assertitself is doing more than just checking a condition. It’s responding to the condition by either throwing an error or allowing the code path to continue. It uses an:operator, but not as part of the? :operator. So I think what you’re trying to do is this:This is basically saying:
This will raise an
AssertionErrorwith the message"assertion failed"(which, naturally, you’d want to replace with a more meaningful and useful message, including any helpful runtime information about the values being examined to help with your debugging).Then, elsewhere, you would handle that
AssertionErrorand respond to it in some way.Using the
assertis very similar to something like this, only shorter and a little more expressive to its intent:As you can see, the
assertis just a little cleaner in that it:ifmight be doing that, or it might be forking off a new code path for any other reason.CustomExceptionto do the same thing, butAssertionErroris more commonly known/expected.