wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Exception class instantiation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Exception class instantiation" Watch "Exception class instantiation" New topic
Author

Exception class instantiation

Dan Drillich
Ranch Hand

Joined: Jul 09, 2001
Posts: 1164
When creating a custom exception class, the programmer instantiates the class.
Who instantiates the library exception class (i.e. IOException). Is it the JVM?
Thanks,
Dan

William Butler Yeats: All life is a preparation for something that probably will never happen. Unless you make it happen.
kyle amburn
Ranch Hand

Joined: Jul 29, 2001
Posts: 64
Hi-
Somewhere in the code that is being executed there is a call to instantiate an Exception. i.e.
If you use the readObject() method in ObjectInputStream, this throws an IOException. If you were to look at the source code, somewhere in the readObject() method or its calls to other methods (say in FileInputStream class) would be a statement such as if (someCondition) throw new IOException(some paramaters);
This is where the Exception is instantiated.
Kyle
Dan Drillich
Ranch Hand

Joined: Jul 09, 2001
Posts: 1164
Kyle,
Let�s say we have the following code.
Where is the Exception class being instantiated?
public void divide(int a, int b) {
try {
int c = a / b;
} catch (Exception e) {
System.out.print("Exception ");
}
}
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

Hope Kyle doesn't mind,

Yes, the runtime will throw that exception for you. This is an example of a RuntimeException, which is unchecked. This means that we aren't obliged to enclose the operation that *might* throw one of these unchecked exceptions into a try-catch block.
Dan Drillich
Ranch Hand

Joined: Jul 09, 2001
Posts: 1164
Thanks, I got it ;-)
My question is where the Exception class is being instantiated?
I don�t see any new Exception()
Jason Ford
Ranch Hand

Joined: Aug 02, 2001
Posts: 54
The exception is instantiated by the method that throws the exception.

Think about string concatenation.
a + b + c
The + symbol is just a convenient way of referring to the method. Under the covers, the method the symbol refers to is called. So what method does / refer to? I don't know: it's a black box, and that's ok with me.
Jason
[This message has been edited by Jason Ford (edited August 15, 2001).]


SCJP, SCWCD
Dan Drillich
Ranch Hand

Joined: Jul 09, 2001
Posts: 1164
Now I got it. Thanks!
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

You don't see any new Exception(), because it's not up to you to do the throwing.

As Kyle mentioned, let's say you have the following:

Now if you cracked open Sun's implementation of the DecimalFormat class, or actually, the NumberFormat class, since that is the abstract super class that contains the method parse with one String parameter, and looked at the parse method, you'd see that the smarty-aleck Sun engineer that created this class did indeed code "throw new ParseException()"

But in the case of RuntimeExceptions, these exceptions are 'unchecked' and are therefore somewhat different than the other ones.

In this case, the exception is the result of an un-anticipated operation, like dividing a number by zero. When the JVM encounters this, it will do the instantiation of the new Exception and throw it up the call stack. Since you probably are not catching this kind of exception anywhere (because you aren't required too...) it percolates all the way up to the main() method... and as I like to think of it.. it pops the entire call stack and you now see it as a stack trace on System.err

So you do not need to throw the Exception, nor do you see it in the code anywhere, because it is the JVM that throws it for you.


 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Exception class instantiation
 
Similar Threads
how to catch an exception that is occuring in an another class
Doubt in servlet API
DataClient Adapter pattern
Dependency vs Association
jsp:useBean InstantiationException