highlighted with try catch: unexpected token.

What could be the problem?

public class Class7 { String[] mas = new String[10]; try { System.out.println(" "); mas[10]=" "; } catch (ArrayIndexOutOfBoundsException e) { System.out.println("Выход за пределы массива"); } Class7(int length) { this.mas = new String[length]; } } 
  • four
    The try/catch construct must be inside a method. - Igor

1 answer 1

Your try / catch is in the class description, not in the method description. Try this:

 public class Class7 { String[] mas = new String[10]; Class7(int length) { this.mas = new String[length]; try { System.out.println(" "); mas[10]=" "; } catch (ArrayIndexOutOfBoundsException e) { System.out.println("Выход за пределы массива"); } } }