aspose file tools*
The moose likes Beginning Java and the fly likes Where are the exceptions thrown? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Where are the exceptions thrown?" Watch "Where are the exceptions thrown?" New topic
Author

Where are the exceptions thrown?

Sky Wallen
Greenhorn

Joined: Mar 17, 2013
Posts: 12
Part of Code:



I understand this throws the exception so that other classes know something wrong has occured. Where exactly is this exception thrown, however? Is it thrown to the parent class? If so, what if I want the exception to be thrown to other classes as well?

Help would be much appreciated! Thanks!
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

Sky Wallen wrote:Part of Code:



I understand this throws the exception so that other classes know something wrong has occured. Where exactly is this exception thrown, however? Is it thrown to the parent class? If so, what if I want the exception to be thrown to other classes as well?

Help would be much appreciated! Thanks!


You may understand that it throws an exception. But I don't. Because I cannot see the rest of your code. I don't know what is loanText etc.. Please describe the complete problem case.


~ Mansukh
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38412
    
  23
Why are you using floats? They are neither the correct way to do money arithmetic (you can find the correct way here), nor are they the correct way to do floating‑point arithmetic, which is to use a double.
Are you using a String to store the value? That is usually a bad idea. If it is a number, use a number class.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Sky Wallen wrote:Where exactly is this exception thrown, however? Is it thrown to the parent class?


It is thrown to the nearest enclosing try block that has an associated catch that can handle it. It might be right in the current method, or in that method's caller, or several levels up the call chain, or, if none of the Java code from main() on down catches it, then it's thrown to the JVM, which prints the stack trace and exits.

If so, what if I want the exception to be thrown to other classes as well?


Depends what you mean by "other classes." You can't just throw it to some arbitrary point in your code. If you're talking about other classes on the current call stack, you don't really have much control over that either, nor should you. If some method up the chain has decided it wants to catch that exception, it's not your place to tell it that it can't. You can increase the likelihood that any given element of the call stack won't catch it by making it a checked exception, especially an Error, but that's a misuse of the class hierarchy, and you can't predict for certain how far it will go.


What are you actually trying to accomplish?
Sky Wallen
Greenhorn

Joined: Mar 17, 2013
Posts: 12
First off, thanks for the responses.

I'm not trying to accomplish something with this code, but more so trying to understand the concept of throwing and catching exceptions between different classes. The code I pasted was just an example in my book of how one class can throw an exception, so that other classes are also notified of the exception.

Basically, my book gives the following scenario. There is a hypothetical program called CreditCardChecker which uses a class called CheckDatabase. While CheckDatabase does its checking, it encounters an exception and handles the error by itself, but the CreditCardChecker class doesn't know the exception took place at all. The part of the code I pasted was an example how CheckDatabase might throw the exception. Basically, I'm wondering how the exception could be thrown to the CreditCardChecker class and how the CreditCardChecker class should catch this exception.

Sorry if this is confusing. I'm fairly befuddled as well, haha!
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

Sky Wallen wrote:First off, thanks for the responses. ...
Basically, I'm wondering how the exception could be thrown to the CreditCardChecker class and how the CreditCardChecker class should catch this exception.

Sorry if this is confusing. I'm fairly befuddled as well, haha!


When you say, it catches the Exception object that occurred while calculating the principal and then re-throws it using throw keyword to the calling method and then up the class hierarchy till it reaches main() method. What this throw keyword does is something you should read here I would suggest you study exceptions and their propagation mechanisms in detail. Revert back if you have any doubts.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14114
    
  16

Sky Wallen wrote:I'm not trying to accomplish something with this code, but more so trying to understand the concept of throwing and catching exceptions between different classes.

You don't really throw exceptions "between classes".

When you run a program, methods are called, which in turn call other methods, which again call other methods, etc. So at any point in the program you're inside a method that has been called by some other method, which has been called by a method above that, etc., up to the main() method which is at the highest level. It doesn't matter in which classes those methods are. When you throw an exception, control goes to the nearest catch block in the stack of methods that has been specified to catch the exception. If the method in which the exception is thrown does not have a catch block, then it jumps up to the method that called that method, and if that doesn't have a catch block either, it jumps up a level again, etc., until a catch block is found. If it eventually reaches main() without encountering a catch block, the JVM prints the stack trace of the exception and your program stops.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Where are the exceptions thrown?