• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Assertions

 
Ranch Hand
Posts: 203
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
5.28 Given the following method, which of these statements will throw an exception,
assuming assertions are enabled?
static int inv(int value) {
assert value > -50 : value < 100;
return 100/value;
}
Select the two correct answers.
(a) inv(-50);
(b) inv(0);
(c) inv(50);
(d) inv(100);
(e) inv(150);
While AssertionError is not an exception but an error. The answer to the above questions should be only B where we get Arithmetic Exception.
For answer A we get AssertionError.
Answers given are:
5.28 (a) and (b)
Statement (a) will cause the assert statement to throw an AssertionError, since (-
50 > -50) evaluates to false. Statement (b) will cause the expression 100/value to
throw an ArithmeticException, since an integer division by zero is attempted.
[ March 15, 2003: Message edited by: Sarma Lolla ]
 
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was curious to see how the JLS classified subclasses of Error. Are they exceptions?

Those unchecked exception classes which are the error classes (Error and its subclasses) are exempted from compile-time checking because they can occur at many points in the program and recovery from them is difficult or impossible. JLS 11.2.1


The possible exceptions in a program are organized in a hierarchy of classes, rooted at class Throwable (�11.5), a direct subclass of Object. The classes Exception and Error are direct subclasses of Throwable. The class RuntimeException is a direct subclass of Exception. JLS 11.5



The class Error and its subclasses are exceptions from which ordinary programs are not ordinarily expected to recover. JLS 11.5

 
Sarma Lolla
Ranch Hand
Posts: 203
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Marlene,
So JLS itself is using the word exception to include both errors, run time exceptions and checked exceptions.
The problem is if you know how many answers you need to select to this question the question wouldn't arise.
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, the JLS categorizes both classes Error and Exception as exceptions. (This surprised me, too.) But the assert spec assert-spec.html and the assert guide assert.html never refer to AssertionError as an exception.
 
Sarma Lolla
Ranch Hand
Posts: 203
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Yes, the JLS categorizes both classes Error and Exception as exceptions. (This surprised me, too.) But the assert spec assert-spec.html and the assert guide assert.html never refer to AssertionError as an exception.


Hmmmmmm after thought for SUN?
reply
    Bookmark Topic Watch Topic
  • New Topic