There is a program that opens a file, reads text from it and splits it into pages. These pages can be scrolled back and forth with the Enter and Backspace keys. Sooner or later, there will be an exceptional situation when we want to look at the next one (go beyond the limits) on the last page of the book, or we want to go back one page to the 1st page Scrolling methods:

public void nextP() { // if (pageNumber == page.size() - 1) { // } else pageNumber++; for (int i = 0; i < page.get(pageNumber).size(); i++) System.out.println(page.get(pageNumber).get(i)); } public void backP() { // if (pageNumber == 0) { // pageNumber++; // } else { pageNumber--; for (int i = 0; i < page.get(pageNumber).size(); i++) System.out.println(page.get(pageNumber).get(i)); } 

Here I have documented a “check”, why do we need this if we can catch the error (am I thinking correctly?). The block listener calling the above methods:

 try { addKeyListener(new KeyAdapter() { @Override public void keyPressed(KeyEvent e) { if (e.getKeyCode() == e.VK_ENTER) { nextP(); } if (e.getKeyCode() == KeyEvent.VK_BACK_SPACE) { backP(); } } }); } catch (IndexOutOfBoundsException e) { System.out.println("Выход за пределы книги") } 

And all the same takes off IndexOutOfBoundsException. How to correctly make it so that this error does not crash?

Ideally, to achieve such a result, while on the last page we pressed Enter (go to the next (non-existent) page), but the program did not react at all (did not display the last page again) and continued to show that very last page.

  • one
    You think wrong. It is cheaper and more correct to check the boundary conditions (your commented code) than to handle the exception. - a_gura
  • Thanks, I will adopt. If you use the commented code, the program will display, for example, the last page again. Please tell me how to make the program not react at all in such an exceptional situation (being on the last page, press Enter and nothing happens (no errors, no page update). - javadranik '13
  • Apparently it is necessary to rethink the algorithm, so that the index does not go beyond the border of the array. - avp
  • The error is not caught because you catch it when you try to add a listener to the button, and not in the processing of the click itself. - cy6erGn0m
  • >> It is cheaper and more correct to check the boundary conditions (your commented code) than to handle the exception not always, sometimes it is better to catch the exception. - Vitaliy

3 answers 3

Maybe just interrupt the method? Don't you have to do anything?

 public void nextP() { if (pageNumber == page.size() - 1) { return ; } else pageNumber++; for (int i = 0; i < page.get(pageNumber).size(); i++) System.out.println(page.get(pageNumber).get(i)); } public void backP() { if (pageNumber == 0) { return ; } else pageNumber--; for (int i = 0; i < page.get(pageNumber).size(); i++) System.out.println(page.get(pageNumber).get(i)); } 
  • Thank you very much! Simple and effective. Exactly what is needed! - javadranik

Like this:

 public void nextP() throws IndexOfBoundsException { pageNumber++; for (int i = 0; i < page.get(pageNumber).size(); i++){ System.out.println(page.get(pageNumber).get(i)); } } public void backP() throws IndexOfBoundException { pageNumber--; for (int i = 0; i < page.get(pageNumber).size(); i++){ System.out.println(page.get(pageNumber).get(i)); } } 

The pageNumber variable must be passed to the methods as a parameter, and reduced in the calling code. And write cycles with braces, so easy to get confused, the number of lines is not an indicator.

  • I did not quite understand your line: “The pageNumber variable must be passed to methods as a parameter”. public class MyFrame extends JFrame {private int pageNumber = 0; public void nextP () throws IndexOutOfBoundsException {... Did you mean that? Block try-catch and leave? The error still crashes ( - javadranik
  • Yes, try-catch to leave and if you have an error that you can not handle, bring the class code (and not the methods separately) and stackTrace errors, but for now talk about nothing. - almagnit

Not there catch an exception:

  addKeyListener(new KeyAdapter() { @Override public void keyPressed(KeyEvent e) { try { if (e.getKeyCode() == e.VK_ENTER) { nextP(); } if (e.getKeyCode() == KeyEvent.VK_BACK_SPACE) { backP(); } } catch (IndexOutOfBoundsException e) { System.out.println("Выход за пределы книги") } } });