• 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

Question about exception

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi!

I have the following piece of code:

public Object remove() {

Object firstOne = head.obj;

head = head.next;
if (head == null)

tail = null;

size--;

return firstOne;

}

The method remove() can throw NoSuchElementException, if the queue is empty.
My question is, how I can write this?
I did the following, but I cannot make it to compile:

public Object remove() {

Object firstOne = head.obj;

head = head.next;

if (head == null)

tail = null;
size--;

return firstOne;

throws java.util.NoSuchElementException {

throw new java.util.NoSuchElementException("exception");

void mindException() {

try {

remove();

} catch (java.util.NoSuchElementException e) {

}

}

}

}

Thank you!

Vlada
 
Ranch Hand
Posts: 815
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you put your code inside "code" tags (the button for which is just below the writing box), your formatting (full of beutiful tabs, I'm sure) will be preserved, and lazy people like me will be able to read your code 100x easier, and be far more likely to answer your question.
 
Nick George
Ranch Hand
Posts: 815
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You declare that the method throws the exception right when you declare the method.

 
Vlada Den
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for reply, George. But where can I declare, that the method throws an exception?

public Object remove() {
Object firstOne = head.obj;
head = head.next;
if (head == null)
tail = null;
size--;
return firstOne;
}
throws java.util.NoSuchElementException {
throw new java.util.NoSuchElementException("exception");
void mindException() {
try {
remove();
} catch (java.util.NoSuchElementException e) {
}
}
}
}

Vlada
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Vlada Den:
Thank you for reply, George. But where can I declare, that the method throws an exception?

public Object remove() {
Object firstOne = head.obj;
head = head.next;
if (head == null)
tail = null;
size--;
return firstOne;
}
throws java.util.NoSuchElementException {
throw new java.util.NoSuchElementException("exception");
void mindException() {
try {
remove();
} catch (java.util.NoSuchElementException e) {
}
}
}
}

Vlada


I think he already answered your question. The "throws" clause goes immediately at the end of the method header and right before the opening { for the method body. The "throw new java.util.NoSuchElementException()" call can go anywhere you like to have it in the method body. When I can, I check for error conditions at the beginning so I can throw exceptions as soon as possible.

Also please use UBB CODE tags to preserve your code formatting. As Nick says, people are more likely to help if they can easily read your code.

Layne
 
Vlada Den
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot, Layne!

Vlada
 
To avoid criticism do nothing, say nothing, be nothing. -Elbert Hubbard. Please critique this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic