Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

Assertion Doubt

 
Ranch Hand
Posts: 182
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For example, you're never supposed
to handle an assertion failure. That means you shouldn't catch it with a catch
clause and attempt to recover. Legally, however, AssertionError is a subclass
of Throwable, so it can be caught. But just don't do it! If you're going to try to
recover from something, it should be an exception. To discourage you from trying
to substitute an assertion for an exception, the AssertionError doesn't provide
access to the object that generated it. All you get is the String message.
<From Chapter 5 K&B>

Can anyone explain me what's the point in this???
What happens if we catch an assertionError using try



OUTPUT : haijava.lang.AssertionError: helloAssertError // didn't understand how it comes.......
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Catching an AssertionError is legal and behaves as a normal try/catch block. You can catch any Throwable anytime. Your code is correct, it compiles and runs perfectly, printing the output you posted.

But it's not very wise to catch assert errors, because there's nothing you can do inside the catch block. Asserts should be used to test code invariants and an assertion error usually says that there is a problem with your code. The best practice with asserts is to let the error stop de program (without catching them) and then fixing the problem in your code.

That is, you can catch AssertionError and nothing special occurs, but it's a bad practice.
 
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, assert is not a tool for exception throwing/handling and therefore it is not appropriate to use it like that.
But its perfectly legal to do so in the meaning of language. You SHOULD use it to validate some expression and its handy in testing and i think it should be transparent when you
look at the flow control of your program. But again, you can write whole application based on assertion but that is just misuse of certain tool.
 
Ranch Hand
Posts: 324
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Banu Chowdary wrote:To discourage you from trying
to substitute an assertion for an exception, the AssertionError doesn't provide
access to the object that generated it. All you get is the String message.
<From Chapter 5 K&B>

Can anyone explain me what's the point in this???
What happens if we catch an assertionError using try



OUTPUT : haijava.lang.AssertionError: helloAssertError // didn't understand how it comes.......




AssertionError does not allow you to pass the cause of the error. If you look at the constructors, there only allow you to pass a message.

In Error(API)..we have.

Error(String message, Throwable cause)
Constructs a new error with the specified detail message and cause.

Error(Throwable cause)
Constructs a new error with the specified cause and a detail message of (cause==null ? null : cause.toString()) (which typically contains the class and detail message of cause).


There is no constructor like above in AssertionError..

In you code, you are using second version of assertion

assert Expression1 : Expression2 ;

if Expression1 is false, its supposed to throw AssertionError. Expression 2 is used as a message that you are getting as output.
 
Ranch Hand
Posts: 352
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If the reason for handling assertion errors is simply to print out an error message, I believe JUNIT or assertion in Java 5 allows custom commenting without the need for try catch does it not? [needs clarification]

Also if assertions are for simple code bug testing, what else would you expect to do with an asertion error, you could not aply any logic to a handle, as if an assertion error is thrown, your code is wrong. Thus the only possible way to handle it would be to manually correct it, which is something the JVM cant do for you I'm afraid

Also don't forget, in order to ensure any assertions are caught they must be switched on at run-time, so even if you place try catch statements for assertions, I don't believe, they would always be exposed.
 
reply
    Bookmark Topic Watch Topic
  • New Topic