• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Exception class instantiation

 
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ");
}
}
 
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, I got it ;-)
My question is where the Exception class is being instantiated?
I don�t see any new Exception()
 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).]
 
Dan Drillich
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Now I got it. Thanks!
 
Mike Curwen
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.


 
reply
    Bookmark Topic Watch Topic
  • New Topic