Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question about exception

 
Vlada Den
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • 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
 
Nick George
Ranch Hand
Posts: 815
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Thanks a lot, Layne!

Vlada
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic