aspose file tools*
The moose likes Beginning Java and the fly likes Question about exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Question about exception" Watch "Question about exception" New topic
Author

Question about exception

Vlada Den
Ranch Hand

Joined: Dec 19, 2004
Posts: 32
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

Joined: Apr 04, 2004
Posts: 815
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.


I've heard it takes forever to grow a woman from the ground
Nick George
Ranch Hand

Joined: Apr 04, 2004
Posts: 815
You declare that the method throws the exception right when you declare the method.

Vlada Den
Ranch Hand

Joined: Dec 19, 2004
Posts: 32
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

Joined: Dec 06, 2001
Posts: 3061
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


Java API Documentation
The Java Tutorial
Vlada Den
Ranch Hand

Joined: Dec 19, 2004
Posts: 32
Thanks a lot, Layne!

Vlada
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question about exception
 
Similar Threads
help me please??
on calling methods..
Reversing a doubly linked list
Saving to a txt file
Queue in Java