dorothy
dorothy

Reputation: 1243

What's the deal with Java's return type in this example

I keep getting method must return result of type byte[]

public class MyClass {

  public static byte[] someMethod() {
    try {
        ...
        byte[] someByte = .... ;            
        return someByte;
    } catch (Exception e) {
        e.printStackTrace();
    } 
    return someByte;
   }    
}

How can I properly return something if there is try/catch block?

Upvotes: 1

Views: 109

Answers (7)

Rebugger
Rebugger

Reputation: 41

As others have said, you must declare someByte outside of the try/catch block. However, it may be useful to explain why.

The try section of a try/catch block is what is executed during normal operation where there are no exceptions thrown. If an exception is thrown during the execution of a try block, execution will immediately stop. From there, execution will continue in a corresponding catch block (Should an appropriate one be found). To put it simply, it is as if the unused code is simply cut out and the code from the catch block replaces it.

The compiler checks for issues that may arise from, what is essentially, self-modifying code. Although in your example, there may be no chance of someByte not being declared, the compiler is unable to say for certain that it will not. it cannot predict whether an exception will be thrown before

byte[] someByte = .... ;

It is taking the possibility of this occurring:

public class MyClass {

  public static byte[] someMethod() {
        ...    // Exception thrown somewhere here
        byte[] someByte = .... ;            
        return someByte;
    } catch (Exception e) {
        e.printStackTrace();
    } 
    return someByte;
   }    
}

Which means that, essentially, what the JVM acts upon is this:

public class MyClass {

  public static byte[] someMethod() {
        ...    // Exception thrown somewhere here
        e.printStackTrace();
        return someByte;
  }    
}

If you were to just have typed this into your source file, you would see the obvious issue, you are returning a variable that simply doesn't exist.

By declaring the variable outside of the try block, you are making 100% sure that the variable will be declared with the correct data type and therefore you and the compiler knows that there will be something to return, even if it is just the default value for your variable. You can still alter the value of the variable inside your try/catch block if you wish.

Upvotes: 0

Sumit Jhajharia
Sumit Jhajharia

Reputation: 135

Here comes the SCOPE OF VARIABLE in picture,basically scope of a variable is the area of code or the number of lines in code where the variable can accessed with the same value.

so here you declared somebyte[ ] inside try block and the scope of variable is only inside the curly braces ,and it is only visible inside that block because you declared it inside the block so when you are trying to get the value of variable out of block will not get expected results.

       try {
    ...
    byte[] someByte = .... ;            
    return someByte;
    } catch (Exception e) {
    e.printStackTrace();
     } 

so declare it outside the block so the variable is visible to that line of code from where you can access it .

you declare it inside the block so its lifetime and scope is also inside the block.

Upvotes: 0

sujithvm
sujithvm

Reputation: 2411

Your array someByte is out of scope

public class MyClass {

  public static byte[] someMethod() {
    try {
        ...
        byte[] someByte = .... ;            // scope begins        
        return someByte;                    // scope ends
    } catch (Exception e) {
        e.printStackTrace();
    } 
    return someByte;                        // out of scope
   }    
}

instead do this

public class MyClass {

  public static byte[] someMethod() {
    byte[] someByte = null ;         // scope begins
    try {          
        someByte = .... ;         
        return someByte;
    } catch (Exception e) {
        e.printStackTrace();
    } 
    return someByte;                // scope ends
   }    
}

Upvotes: 2

Reimeus
Reimeus

Reputation: 159774

Declare someByte outside the scope of the try/catch block so that it's visible within the wider scope of the method

byte[] someByte = null;
try {
    someByte = .... ;            
} catch (Exception e) {
    e.printStackTrace();
} 
return someByte;

Upvotes: 1

Santosh Joshi
Santosh Joshi

Reputation: 3320

move byte[] someByte outside the try block

public class MyClass {

      public static byte[] someMethod() {
        byte[] someByte = .... ;
        try {
            ...
            byte[] someByte = .... ;            
            return someByte;
        } catch (Exception e) {
            e.printStackTrace();
        } 
        return someByte;
       }    
    }

Upvotes: 1

Mustafa sabir
Mustafa sabir

Reputation: 4360

To properly return something it must be declared outside try{} scope , and initialized in it (If that requires you to include try{} ) . For example :-

public class MyClass {

  public static byte[] someMethod() {
    byte[] someByte =null;
       try {
        ...
        someByte = .... ;
    } catch (Exception e) {
        e.printStackTrace();
    } 
    return someByte;
   }    
}

Upvotes: 4

Eran
Eran

Reputation: 393821

This won't compile, since someByte is declared within the try block and accessed outside it.

Should be :

public class MyClass {
  public static byte[] someMethod() {
    byte[] someByte = null;
    try {
        ...
        someByte = .... ;            
        return someByte;
    } catch (Exception e) {
        e.printStackTrace();
    } 
    return someByte;
  }    
}

Upvotes: 4

Related Questions