Creating and Throwing Exceptions (basic)
Hi,
Basically, I want to create an exception and throw it. It's just a basic exception that will give an error message. I couldn't find anything useful in the Java tutorial at sun.com, so I'm posting here.
I want an exception I create, NoPictureException, to be thrown under a certain condition. The created exception is incredibly basic; I'm just testing it out. However, I do not know how to throw it properly. I'm not sure if I'm creating the exception properly either. Here is my exception code:
// No pic exception
publicstatic Exception NoPictureException(){
Exception nopic =new Exception();
nopic.printStackTrace();
return nopic;
}
I want this to be thrown here (Warning: the syntax is incorrect here for the exception throw, I do not know what to do):
// Individual method
publicstatic Individual createIndividual(OntModel m, String URI, OntClass c,boolean im, String pic,boolean co, StringBuffer blarg){
Individual myInd = m.createIndividual(URI, c);
if ((im) && (pic==null)){
Exception NoPicture = NoPictureException();
throw NoPicture;
}
elseif (im){
myInd.addProperty(image, createImageResource(kdb, kdbPDCNS + pic));
}
else{
}
if (co){
try{
myInd.addProperty(RDFS.comment, blarg);
}
catch (NullPointerException e){
System.err.println("FileNotFoundException: " + e.getMessage());
}
}
else{
}
return myInd;
}
Basically, I'm not too sure how to do this at all. If anyone can give me advice, I will be grateful.
Thanks,
Dan
[2868 byte] By [
Djaunla] at [2007-10-3 2:33:41]

> > class NoPictureException extends Exception {
> }
>
That defines an exception class which is named NoPictureException
> > class Example {
> public void createIndividual() throws
> NoPictureException {
>if (true) {
>throw new NoPictureException ();
>}
> }
>
>
The example above shows that a method which wants to throw a checked exception my declare that it is going to throw an exception (using throws). The line that says throw new NoPictureException() is the line which throws the exception.
kajbja at 2007-7-14 19:32:40 >

Let me actually change my question a bit, because I'm not really understanding how to create an exception and throw it. What if I want to throw an already defined exception, such as IllegalArgumentException? I could say something like:
public class IllegalArgumentException extends RuntimeException {
}
Correct? However, how would I throw it appropriately, as I tried to explain in my original post? I've looked around on the internet for a tutorial on throwing exceptions, but I can't seem to understand it. If I want my exception to be thrown like this:
if ((im) && (pic==null)) {
// THROW EXCEPTION HERE
}
How would I do this? Sorry for my newbishness.
Thanks,
Dan
> > if(SOMETHING_IS_WRONG){
>throw new IllegalArgumentException();
>
>
>
> That'll throw an exception for you....why not play
> around with it for a bit.
Well, that's what I originally thought. However, I get a syntax error when I try that. I'll post the error here, but I have no idea what to make of it (OMEGAEP is my file...OMEGAEP.java):
No enclosing instance of type OMEGAEP is accessible. Must qualify the allocation with an enclosing instance of type OMEGAEP (e.g. x.new A() where x is an instance of OMEGAEP).
It says I need a new instance of my main class or something like that. At this point, I have no idea what it's talking about.
Also, thanks for the link mchan0, I will take a look in a minute or so.
I'm all out of ideas, and I fear this may be getting too complex. If anyone has new ideas, I'm all ears. Maybe that link will clear things up; I'll take a look.
Dan