简体   繁体   中英

Java generate random solution

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
}

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.
}

Catch the StackOverflowError exception.

boolean success = true;
do {
  try {
     success = true;
     someRecursiveFunction();
  } catch (StackOverflowError e) {
     success = false;
  }
} while (!success);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM