Skip to content
Snippets Groups Projects
Commit c2831335 authored by leinolm's avatar leinolm
Browse files

hieman parempi poikkeusten käsittely

parent 56d3cd33
Branches main
No related tags found
No related merge requests found
...@@ -18,16 +18,18 @@ public class ConsoleProcess extends Thread{ ...@@ -18,16 +18,18 @@ public class ConsoleProcess extends Thread{
private PrintStream output; private PrintStream output;
private Scanner input; private Scanner input;
private Commands komennot; private Commands komennot;
private String prompt;
/** /**
* initializes the class * initializes the class
* @param in Stream that the thread will read from * @param in Stream that the thread will read from
* @param out Stream that the thread will write into * @param out Stream that the thread will write into
* @param prompt What gets displayed in the stream for prompt, eg "console>"
*/ */
public ConsoleProcess(InputStream in, OutputStream out) { public ConsoleProcess(InputStream in, OutputStream out, String prompt) {
this.output = new PrintStream(out); this.output = new PrintStream(out);
this.input = new Scanner(in); this.input = new Scanner(in);
komennot = new Commands(); komennot = new Commands();
this.prompt = prompt+">";
} }
/** /**
...@@ -36,14 +38,10 @@ public class ConsoleProcess extends Thread{ ...@@ -36,14 +38,10 @@ public class ConsoleProcess extends Thread{
@Override @Override
public void run() { public void run() {
while (true) { while (true) {
output.print("Cowhello>"); output.print(prompt);
String syote = input.nextLine(); String syote = input.nextLine();
if (syote.equals("")) continue; if (syote.equals("")) continue;
try { exec(syote);
exec(syote);
} catch (Exception e) {
puhu(e,"Virhe komennon kanssa: "+e.getMessage());
}
} }
} }
...@@ -91,7 +89,11 @@ public class ConsoleProcess extends Thread{ ...@@ -91,7 +89,11 @@ public class ConsoleProcess extends Thread{
private void exec(String a) { private void exec(String a) {
String[] jutut = a.split(" "); String[] jutut = a.split(" ");
komennot.exec(output,jutut); try {
komennot.exec(output,jutut);
} catch (Exception e) {
puhu(e,"Virhe komennon kanssa: "+e.getMessage());
komennot.exec(output, new String[]{"help", jutut[0] });
}
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment