-1

I just learned the Exceptions in Java, but I wonder whether "may not initialized variable" is an Error or Exception or something else?

I tried to treat it as an Exception and I write my code in notepad++ like this:

public class Demo{
    public static void main(String[] args) throws Exception{
        int a ;
        System.out.println(a);
    }
}

It didn't work, I still can't compile it using "javac Demo.java". It shows that it is not an Exception.

I have not learned "Error" before, but I search "Error" in the API, check the subclass of Error, I still don't know the answer.

Is there someone could help me? Many thanks.

~~~~~~~~~~~~~~~~~~~~~~~

Well, I know I should assign a value to a, what I want to know is the question I asked.

I learned that sentence "A local variable must be declared and assigned a value before it can be used." in Y.Daniel Liang's book , please don't use that sentence to answer me , it is not the answer to my question , thank you.

~~~~~~~~~~~~~~~~~~~~~~~

let's see another demo. It is about compile Exception.

public class Demo2{
    public static void main(String[] args) throws ParseException {
    //public static void main(String[] args){
        String str = "1991-0101";
        DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date birthdayDate = sdf.parse(str);
        System.out.println(birthdayDate);
    }
}

The ParseException is a compileException (it extends Exception), when I didn't add the "throws ParseException", I can't compile it. After I add "throws ParseException", I CAN compile it, although it still can't run.

But when I add the "throws Exception" in the first Demo, I still can't compile it. This is the really point that makes me confused.

"It is a compilation error, not an exception", sorry I don't know how to talk to you in stackoverflow, but I think you are right, could you give me some keywords so that I can search in google or stackoverflow, or should I spend some time learn compilers?

wayne
  • 3
  • 4
  • 6
    It's a compilation error. Not an exception. – Eran Jun 07 '18 at 09:38
  • 2
    Possible duplicate of [Java variables not initialized error](https://stackoverflow.com/questions/14484550/java-variables-not-initialized-error) – Jai Jun 07 '18 at 09:38
  • this: "int a ;" combined to: "may not be initialized" ... how does this confuse you? – Stultuske Jun 07 '18 at 09:39
  • You're trying to print out the value of an uninitialized variable `int a`. This is clearliy a compilation error. Your code is supposed to be compiled before it is executed. You can receive a runtime exception when it's executed. – Hakan Dilek Jun 07 '18 at 09:49

2 Answers2

0

An exception is something (usually unusual) which happens during runtime.

There are checked exceptions which must be dealt with, because they can happen at any time (e. g. IOError).

There are also unchecked exceptions which don't happen in a "clean" program (e. g., if you check all references before you use them, you won't get a NullPointerException; if you check all array indexes carefully, you won't get an OutOfBounds error etc.)

Both these happen at runtime.

What you experience here is an error which is visible at compile time, that there is a possibility that the variable is not initialized. That cannot be tolerated by the compiler and the program doesn't compile, and so it cannot run, and as said, runtime is the time where exceptions become relevant and so you cannot treat it as an exception.

glglgl
  • 89,107
  • 13
  • 149
  • 217
  • Thank you, I'v learned a lot. But there are still something make me confused, and maybe I have to point some little problem. – wayne Jun 08 '18 at 08:49
  • Your last paragraph made me clear about "may not initialize variable", I'm very graceful to you , and I still want to know something about "errors", so that when I happen to meet errors next time, I can understand it clearly. – wayne Jun 08 '18 at 08:56
  • And I found maybe there's something wrong: "RuntimeException, Error, and their subclasses are known as unchecked exceptions. All other exceptions are known as checked exceptions, meaning that the compiler forces the programmer to check and deal with them in a try-catch block or declare it in the method header."...."Unchecked exceptions can occur anywhere in a program. To avoid cumbersome overuse of try-catch blocks, Java does not mandate that you write code to catch or declare unchecked exceptions." That's what I found in Y.Daniel Liang's [Introduction to Java programming] Page 457. – wayne Jun 08 '18 at 09:05
  • @wayne That's exactly what I already mentionned. These are errors during runtime, when a program that could be built is run, i. e. when executing the program. The error you are facing results from an unability to compile the program. – glglgl Jun 08 '18 at 12:45
0

In compiled languages like java, we use the terminology compile time and runtime

Compile time is where the compiler may throw errors, if it finds that the code is somehow wrong. This can be syntactically wrong or, for example if a variable is not initialized when used. Basically, the compiler finds everything that can be known to go wrong before the program is run.

At runtime (meaning when you run the program), exceptions are thrown. Exceptions may be stuff like zero division (where the zero came from a variable), that the compiler has no way of knowing before runtime.

You can (and i recommend you to do) read more about runtime and compile time here: Runtime vs Compile time

Just as a quick sidenote. Azhy's answer in which he said that a is 0 in your code is wrong.

It would be true if the 'a' variable was a field (fancy word for a variable in a class), then yes, it would have defaulted to 0, but as it was a local variable (a variable in a method (in your case, the main() method)), it just doesn't have a value, and can therefore not be printed.


In your second example, the compilers sees that the parse() method you call on line 6 of your example, is declared to throw a an exception named ParseException. The declaration of the parse method probably looks something like this:

public Date parse() throws ParseException

This means, that when the compiler sees the call to parse, it goes "let me just check what parse() does" and then finds out that it has a chance to throw the ParseException. Now the compiler will throw an error unless you handle the possibility of a ParseException being thrown. this can be done by either surrounding the call with try/catch, like this:

public class Demo2{

    public static void main(String[] args) {
        String str = "1991-0101";
        DateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        //pulled birthdayDae out of the try/catch, so it is accesible to the println later
        Date birthdayDate;

        try {
            // tries to do the parse
            birthdayDate = sdf.parse(str);
        }catch(ParseException e){
            // if it fails this will be executed
        }

        System.out.println(birthdayDate);
    }
}

or by declaring that main might throw a ParseException like you did.

the second example is not very beginner friendly as it contains both a runtime exception AND a compile time error that is thrown because of missing exception handling of the runtime exception.

  • Thank you, I am reading, It seems like different from what my teacher teches me. He says that ***exception extends Exception are compile exception(except for class extends RuntimeException). And ***excepion extends RuntimeException are runtimeexception. – wayne Jun 07 '18 at 11:28
  • If this was the answer you were looking for, you should accept it as the right answer by clicking the tick under the upvote/downvote buttons. – Bob McCheatersen Jun 07 '18 at 12:23
  • Thank you very much for your help, I really appreciate that. – wayne Jun 07 '18 at 14:18