简体   繁体   中英

Java: Catch uninitialized variable

Suppose we have a case where a variable is declared in certain conditions:

if (x = 1)
 boolean z = true;

Later on, we'd like to test if the variable z exists with try-catch. Is this possible and if so, what exception should we catch?

try {
 if (z)
  //do smth
} catch (<exception?> ex) {
 //do smth_else
}

Of course it would be possible to declare z before and change it's value accordingly in if block, but just hypothetically, is the above possible in Java? Python for example has NameError that will be raised when accessed local or global variable is not declared.

Thanks!

HSI.

What if you declared your variable like this:

Boolean x = null;

In that case you could check for it being null or not.

An even better alternative would be using an enum to represent the uninitialized value:

public enum MyEnum {
    UNINITIALIZED, TRUE, FALSE;
}

because if you will try to maintain your code several months later you (or someone else) may be puzzled about the Boolean being null.

we'll get compilation error if the variable we are using is not declared or is not visible in current scope. If it is declared we can check for NullPointerException if that was object. In case of primitive data types we should check for default values.

Suppose we have a case where a variable is declared in certain conditions:

Well it is difficult to assume because that would not compile:

  • you should use == to test for equality
  • you can't declare a variable in an if statement unless there is a block

Now assuming you enclose that declaration inside a block, the scope of that variable would be that block and you wouldn't be able to use it in your try / catch block (unless it is inside the if block of course, but I don't think that's what you want).

No, this is not possible in Java. You have to have the variable declared before you can refer to it, otherwise you will get a compilation error.

Boolean z = null;
if (x = 1){
  z = true;
}


if(z == null){
 //not initialized

}else{
  //initialized
}

1, first of all your syntax for java is wrong: it can be following

if (x == 1)
  boolean z = true;

2, in Java You have to declared variable before use it.

it's not possible, Java is a strongly typed programming language because every variable must be declared with a data type before it can be used.

int x = 1;
boolean z = null;

if (x == 1)
 z = true;

try {
 if (z)
  //do smth
} catch (NullPointerException npe ) {
 //do smth_else
}

So far I understand , you wont be able to compile this piece of code. I can not remember any exception class but what I think is even if you "Invent" exception for this type of error. It won't compile. Because default values of primitive types are assigned to uninitialized class variables called fields but for variable used in method body, it gives compile time error

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM