• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 317
  • Last Modified:

How can a program compile and still have an "InvalidDataException" exception?

How can a program compile and still have a "InvalidDataException" exception? Doesn't java check for whether types match up upon compiling? What does this type of exception usually stem from? What should I look for to fix it?

I should also mention that I have no user input, no database access, and no file access going on in the program. It's all pretty much isolated.

Btw, I'm a newb so could you please explain this non-technical layman's terms?
0
Eindoofus
Asked:
Eindoofus
  • 7
  • 3
  • 3
  • +1
11 Solutions
 
CEHJCommented:
Please post the code you're talking about, so as to focus the questions.
0
 
evedderCommented:
java has 2 types of exception, compilation and run time exception, compilation ones checks for syntax primarily, run time exceptions are errors found during execution.

If you are using a date in your code you should check that part, the exception is pretty much saying you the problem. As CEHJ says it would be easier to check the code
0
 
evedderCommented:
mmmm sorry i misread the exception name hehe. is data no date, anyway, could you post your code so we can check what is causing the exception?
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
EindoofusAuthor Commented:
Sorry, but the rest of the code is part of a homework assignment and I would rather not post it. I'm just trying to grasp the ability for code to throw an exception when there is not outside input to the program...

look at things from my perspective as a newbie please...
0
 
EindoofusAuthor Commented:
That's what is driving me crazy. How can I be the problem when the code compiles and there is no outside input??? What potential scenarios can cause this?
0
 
evedderCommented:
well it seems InputDataException is user defined, so you should have a InputDataException class, you can check the code inside or check in your main class if theres any throw for this exception
0
 
CEHJCommented:
>>I'm just trying to grasp the ability for code to throw an exception when there is not outside input to the program...

Are you saying your code IS THROWING an exception?
0
 
CEHJCommented:
Just to clarify, when a method declares that it throws an exception, the reason that it does so is not that it IS GOING TO throw one, just that

a. it can do
and more importantly:
b. that the method that calls it is required to CATCH that exception

http://download.oracle.com/javase/tutorial/essential/exceptions/
0
 
stachenovCommented:
Compiling has nothing to do with exceptions. You can get errors or warnings during compilation, but that's different. Please clarify:

1. What you're trying to achieve.
2. What you're doing to achieve that, preferably with some sample code.
3. What you expect to happen.
4. What actually happens.

For example, "I'm trying to write a program that does A, I have written the following code C which compiles fine but then throws an exception E although I expect it to run successfully with the result B".
0
 
CEHJCommented:
I'm guessing that Eindoofus is asking from a conceptual point of view.

>>Compiling has nothing to do with exceptions.

It has nothing to do with the *occurrence* of exceptions, but otherwise has quite a lot to do with them, and it has a lot to do with them *by design*
0
 
stachenovCommented:
Then I don't get why a concrete exception (InvalidDataException) is mentioned. I don't know what kind of exception is that (don't see it anywhere in the standard lib), but from conceptual point of view the very fact that there is some exception is declared somewhere means that it is *supposed* to be thrown in some situations. Asking "how can a program be compiled and still have an exception" doesn't make much sense. After all, program *has* to be compiled successfully to have any exceptions thrown. As it is, I just don't understand the question.
0
 
CEHJCommented:
Well, we await clarification. In point of fact, InvalidDataException is a custom exception under discussion here and elsewhere
0
 
EindoofusAuthor Commented:
I'm sorry, but this is a badly worded question and I regret posting it. And At this point my program works and I don't know why it was throwing that exception. I don't want to delete this question since I've already made that mistake but I have no idea how to go about rewarding points.

I guess I'm just looking to gain a general conceptual understanding of the reasons why any program would throw any exception if it's not receiving outside input while running.
0
 
CEHJCommented:
I'm quite happy to see a points split. If you study my comments and the link i posted, it should clarify things. There's no need for human input for an exception to occur
0
 
CEHJCommented:
:)
0
 
stachenovCommented:
Well, just in case I understood this question somewhat right, here is a brief overview that may help.

First of all, for a program in almost any imaginable programming language, there are two types of possible errors: compilation and run-time errors. Compilation errors, unsurprisingly, occur during compilation, and the fact you're getting compilation errors means that your program won't even run until you fix them and recompile. If you don't get any compilation errors, it only means that your program is a valid program from language point of view, and that you're able to run it. Runtime errors are those that occur during program execution, and exceptions is the most common way to handle them in Java.

Since you're asking about types matching, I assume that your exception has something to do with data type checking. You're correct that Java does check types during compilation, but it can't check everything. For example, you can't pass a string to a function that expects integer, that will result in a compilation error because the compiler knows that string can't possibly be an integer even when it contains just something like "123" - since there is no automatical string-to-int conversion in Java. However, if some function expects a positive integer and you pass a variable containing -5 to it, then there won't be a compilation error since the compiler can't possibly know what that variable will contain at run-time. Moreover, it doesn't even know that a specific function only accepts positive values - there is no way to declare that in Java when defining a function.

Therefore, exception occuring at run-time only mean that something went wrong somewhere. If you need to know what exactly went wrong, you should see at the stack trace, looking at the first places where the exception occured, then read the documentation of that functions (it should say which exceptions are thrown in which cases), or, if that happened in your code, look carefully your code. If your code has "InvalidDataException", then it is probably meant to be thrown when some sort of invalid data passed somewhere, but that data was not necessarily input by a user - you could have some other code that passes this incorrect data somewhere, and the compiler can't check everything for you. For example:
 
int x;
int y;
// some code that assigns 5 to x and 5 to y in a non-obvious way
int result = calculate(x-y); // supposed to be y-x

// ... in some other place:
int calculate(int basis) {
  if (basis < 0)
      throw new InvalidDataException();
  // code for valid data follows
}

Open in new window

Also note that it is generally not a very good idea to throw a custom exception (InvalidDataException) in such case. Java has IllegalArgumentException just for that. Of course, that is an unchecked exception, and you may want to have a checked exception for your own reasons, but that's outside of the scope of this answer - you should really read the link posted by CEHJ for such details.
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 7
  • 3
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now