0

I am doing scheduling system and it will produce a random timetable, due to random method it will cause some of the exam can't add to the timetable and it will result stack over flow error..... but sometime it will able to produce a complete timetable.

So I want to ask here how can i do like this:

if result == stack over flow error {
    redo the function
}

2 Answers2

0

It's bad idea to have stackoverflow error in your program and change the flow of execution using catch.
But if you want to do so you can surround your code in try catch block like this.

try{
//your code that is likely to produce stackoverflow error
}
catch(StackOverflowError se)
{
//do whatever you want after stackover flow.
}
Deepankar Singh
  • 662
  • 1
  • 9
  • 20
  • may i know what se does? – user3832964 Nov 15 '14 at 18:33
  • whatever is thrown by the try block must be caught by the catch block so you have to specify a reference name in which you're getting that object that is thrown by the try block.In this case I specified name as se in which i'm getting object thrown by try block. – Deepankar Singh Nov 15 '14 at 18:47
  • you can read exception handling in java here http://www.javatpoint.com/exception-handling-in-java – Deepankar Singh Nov 15 '14 at 18:51
-1

Catch the StackOverflowError exception.

boolean success = true;
do {
  try {
     success = true;
     someRecursiveFunction();
  } catch (StackOverflowError e) {
     success = false;
  }
} while (!success);
dieend
  • 2,231
  • 1
  • 24
  • 29
  • This is NOT a solution. Errors are meant to crash the JVM. – Jazzwave06 Nov 15 '14 at 18:21
  • 1
    "An Error is a subclass of Throwable that indicates serious problems that a reasonable application should not try to catch." - https://docs.oracle.com/javase/7/docs/api/java/lang/Error.html – DanielGibbs Nov 15 '14 at 18:21
  • You're answering the question, but then the OP will think this is a neat idea. – Jazzwave06 Nov 15 '14 at 18:23
  • Yeah, I answered this first, got downvote, and another answered like this, not only accepted, but also not downvoted. Today really is a bad day for me. – dieend Nov 15 '14 at 19:57