• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

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
 
Humans and their filthy friendship brings nothing but trouble. My only solace is this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic