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]
# 1

class NoPictureException extends Exception {

}

class Example {

public void createIndividual() throws NoPictureException {

if (true) {

throw new NoPictureException ();

}

}

}

Kaj

kajbja at 2007-7-14 19:32:40 > top of Java-index,Java Essentials,Java Programming...
# 2
Hi,Thanks for the reply. Do you think you could kindly explain what you're doing, so I know how this works? Also, how would I throw it from my createIndividual method when the satisfied condition is met?Thanks,Dan
Djaunla at 2007-7-14 19:32:40 > top of Java-index,Java Essentials,Java Programming...
# 3

> > 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 > top of Java-index,Java Essentials,Java Programming...
# 4
Be careful though. When catching an exception and then re-throwing a new one if you dont' nest the old one you'll lose the original exception and it'll make debugging a real pain.sometimes you WANT to do this....however, sometimes you dont.
Norweeda at 2007-7-14 19:32:40 > top of Java-index,Java Essentials,Java Programming...
# 5

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

Djaunla at 2007-7-14 19:32:40 > top of Java-index,Java Essentials,Java Programming...
# 6

if(SOMETHING_IS_WRONG){

throw new IllegalArgumentException();

}

That'll throw an exception for you....why not play around with it for a bit.

Norweeda at 2007-7-14 19:32:40 > top of Java-index,Java Essentials,Java Programming...
# 7
http://www.onjava.com/lpt/a/4345
mchan0a at 2007-7-14 19:32:40 > top of Java-index,Java Essentials,Java Programming...
# 8

> > 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

Djaunla at 2007-7-14 19:32:40 > top of Java-index,Java Essentials,Java Programming...
# 9

Well, I actually solved it. I had this:

public class IllegalArgumentException extends RuntimeException {

}

And I removed it. So, now it works. Interesting. Just wondering, why would it not work with the above code snippet?

Djaunla at 2007-7-14 19:32:40 > top of Java-index,Java Essentials,Java Programming...
# 10
probably b/c you don't need it b/c it already exists as an exception in java.
Norweeda at 2007-7-14 19:32:40 > top of Java-index,Java Essentials,Java Programming...
# 11
That makes sense, haha. Although I am still pretty new to exceptions, I think this has helped me clear up some of questions I had. Thanks to everyone who responded, I appreciate it.Dan
Djaunla at 2007-7-14 19:32:40 > top of Java-index,Java Essentials,Java Programming...