-1

I have a switch statement:

public Player player(Colour colour, ScotlandYardView view, String mapFilename) {
    switch (typeMap.get(colour)) {
        case AI:
            return new RandomPlayer(view, mapFilename);
        case GUI:
            return gui(view);
        default:
            return new RandomPlayer(view, mapFilename);
    }
}

I get the unhandled IOException error. Why is that and how to I fix it?

Kromster
  • 7,181
  • 7
  • 63
  • 111
Arthur Le Calvez
  • 413
  • 2
  • 7
  • 17

2 Answers2

0

Well, let me at least answer the why-part, in addition to codegasmer's answer: it is simply because any of the methods you call throws an IOException. This could be typeMap.get() (which I would not expect in this case, but threoretically possible), the 'gui' method or the constructor of RandomPlayer.

codegasmer shows a way to identify, which of these methods is responsible, you would see it in the stack trace - and even reveal in which of the methods the method in question called the exception was thrown.

If you can live with simply catching the exception and providing a fallback solution, you could do so (e. g. by creating a fallback object in the catch block). Better is to find out, what was the original cause of the exception and then avoid it, if possible. Using a debugger can help here very much. As I do not know any details of the involved classes, I cannot provide any further help here.

Aconcagua
  • 24,880
  • 4
  • 34
  • 59
-1

Use try catch.You are using method which can throw some exception so compiler is saying you to handle that

public Player player(Colour colour, ScotlandYardView view, String mapFilename) {
try{
    switch (typeMap.get(colour)) {
        case AI:
            return new RandomPlayer(view, mapFilename);
        case GUI:
            return gui(view);
        default:
            return new RandomPlayer(view, mapFilename);
    }
}catch(Exception e){
e.printStackTrace();
}
}
codegasmer
  • 1,462
  • 1
  • 15
  • 47
  • 1
    Does not answer the `why` part of the quesiton. – Chetan Kinger Apr 29 '15 at 10:55
  • Still doesn't explain `why` the `IOException` – Chetan Kinger Apr 29 '15 at 10:56
  • @ChetanKinger I have clearly said `You are using method which can throw some exception so compiler is saying you to handle that` – codegasmer Apr 29 '15 at 10:58
  • I assume you are performing IO operation in some method of RandomPlayer class or may be inside RandomClass constructor, or may be in gui(). So you need to handle the exception on all those caller methods – Lalit Rao Apr 29 '15 at 11:00
  • @ChetanKinger In `gui()` method.Please read the answer carefully before downvoting – codegasmer Apr 29 '15 at 11:00
  • codegamer, how do you know that? Why not the `RandomPlayer` constructor? – Chetan Kinger Apr 29 '15 at 11:03
  • @ChetanKinger because it is not a good practice to put `IO` code in constructor.So I guess it should be the method – codegasmer Apr 29 '15 at 11:05
  • An answer must be definitive. As your answer stands, we still don't know `why` the IOException occurs. The try catch could be put inside the `gui `method or in the `RandomPlayer` constructor or somewhere better. We can't know where unless we know why. – Chetan Kinger Apr 29 '15 at 11:06
  • Possibly not good practice, but still somebody might have done it inspite of what good or bad practice is! – Aconcagua Apr 29 '15 at 11:07
  • Why is it not a good practice? For example, constructor for `FileOutputStream` throws a `FileNotFoundException`, which is an `IOException` – Albert Apr 29 '15 at 11:13