Variable access inside try catch

I keep getting a compilation error in the return menu bar. The line says that there is no menuFont variable. Can someone tell me how to fix this.

import java.awt.Font; import java.awt.FontFormatException; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; public class loadFiles { Font getFont(){ try { Font menuFont = Font.createFont( Font.TRUETYPE_FONT, new FileInputStream("font.ttf")); } catch (FileNotFoundException e) { System.out.println("Cant find file."); e.printStackTrace(); } catch (FontFormatException e) { System.out.println("Wrong file type."); e.printStackTrace(); } catch (IOException e) { System.out.println("Unknown error."); e.printStackTrace(); } return menuFont; } } 
+4
source share
8 answers

The main problem with your code is that the Font object is only in scope for the entire try block, so it is no longer available in your return statement at the end of the method. Two options:

Move the variable declaration outside the try block:

 Font menuFont = null; try { menuFont = Font.createFont(...); } catch (...) { } return menuFont; 

Or, do return Font.creatFont(...) inside try, thereby avoiding the need for a variable in the first place (and, obviously, return null at the end of the method).

+14
source

you must declare a menuFont variable outside ...

 import java.awt.Font; import java.awt.FontFormatException; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; public class loadFiles { Font getFont(){ Font menuFont = null; try { menuFont = Font.createFont( Font.TRUETYPE_FONT, new FileInputStream("font.ttf")); } catch (FileNotFoundException e) { System.out.println("Cant find file."); e.printStackTrace(); } catch (FontFormatException e) { System.out.println("Wrong file type."); e.printStackTrace(); } catch (IOException e) { System.out.println("Unknown error."); e.printStackTrace(); } return menuFont; } } 
+3
source

The scope of the menuFont variable is inside the try block. Instead, move the variable outside. Sort of:

  Font getFont(){ Font menuFont = null; try { menuFont = Font.createFont( Font.TRUETYPE_FONT, new FileInputStream("font.ttf")); } catch (FileNotFoundException e) { System.out.println("Cant find file."); e.printStackTrace(); } catch (FontFormatException e) { System.out.println("Wrong file type."); e.printStackTrace(); } catch (IOException e) { System.out.println("Unknown error."); e.printStackTrace(); } return menuFont; } 

Remember that in case of an exception, this method will return the font null .

+3
source

This is because menuFont does not exist within the getFont method. Variables defined outside your current region / block (a pair of curly braces) are available, while those defined in a nested block are missing. You can fix this in two ways:

  • Move the menuFont above try (as part of getFont ). Since you are expecting an exception in Font.createFont, leave this code inside the try block.
  • Move the return inside the try block.

Check out the answers to Is it finally running in Java? for more subtleties about try / catch / finally.

+3
source

menuFont does not exist outside the try block that your compiler is trying to tell you. Instead, declare it before the try block, and then try to assign it a value inside the try block:

 Font menuFont; try { Font menuFont = ... } ... return menuFont; 
+2
source

This is due to the size of your menuFont variable. You declare it inside try , which means that it will not be accessible anywhere except inside the two curly braces. If you want to have access to it in catch or somewhere outside of try , change the code to something like this:

 Font menuFont = null; try { menuFont = Font.createFont( Font.TRUETYPE_FONT, new FileInputStream("font.ttf")); } catch (FileNotFoundException e) { // you can access menuFont here (too) } //... return menuFont; // <-- note: can be null here 
+2
source

In general, I would do the following:

 import java.awt.Font; import java.awt.FontFormatException; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; public class loadFiles { Font menuFont = null; Font getFont(){ try { menuFont = Font.createFont( Font.TRUETYPE_FONT, new FileInputStream("font.ttf")); } catch (FileNotFoundException e) { System.out.println("Cant find file."); e.printStackTrace(); } catch (FontFormatException e) { System.out.println("Wrong file type."); e.printStackTrace(); } catch (IOException e) { System.out.println("Unknown error."); e.printStackTrace(); } } return menuFont; // could potentially be null! } 

And check for null wherever you call this method.

0
source

Source: https://habr.com/ru/post/1399591/


All Articles