Reputation: 23
I am new to java. I was reading java from sun website. It says,
When an error occurs within a method, the method creates an object and hands it off to the runtime system. The object, called an exception object, contains information about the error, including its type and the state of the program when the error occurred.
My question is,
EDIT : I didn't understand this term "When an error occurs within a method, method creates an object' ?"
Thanks.
Upvotes: 2
Views: 2426
Reputation: 8287
Your Java code is running on a virtual machine; there is more going on than just the code you are writing. The Java SDK throws exceptions.
Exception handling is done in such a way that linear execution of the code is interrupted, and exception flow instead kicks in. Exception flow is basically the exception being 'thrown' until some construct 'catches' it.
try {
... some code is executed
... some code throws exception (throw new Exception())
... some more code is never executed
} catch(Exception e) { // CATCHES
// code executed in case exception is caught
} finally {
// code that runs regardless of above exceptions
}
If catching does not make sense in some local scope, methods can propagate exceptions like this:
public void doSomeWork(..) throws Exception {
}
and the exception can be caught elsewhere, i.e. higher up in logic. Exceptions also are normal objects, and subclasses (finer details) can be caught before superclasses etc.
There is no stack and heap in the traditional sense in Java, so don't worry about that for now - later you can set some memory management parameters for the JVM as a whole, but the nature of those will normally not be reflected in your code.
Upvotes: 0
Reputation: 597114
throw new SomeException()
Upvotes: 4
Reputation: 36852
Basically, you create it.
throw new Exception(); // notice the new
Like any Java object, it is allocated on the JVM heap.
Upvotes: 0