Exception handling - "The import java.io.FileNotFoundException cannot be resolved"

695 views
Skip to first unread message

Unknickable

unread,
Nov 8, 2007, 1:21:38 PM11/8/07
to Google Web Toolkit
Hi,
I implemented a simple RPC. I works fine, but when I try to throw an
exception, I get a compilation error. Here is my code:

MyService:
---------------------
package client;
import java.io.FileNotFoundException;
import com.google.gwt.user.client.rpc.RemoteService;

public interface MyService extends RemoteService {
public String getResult () throws FileNotFoundException;
}


MyServiceImpl:
---------------------
package server;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import client.MyService;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
public class MyServiceImpl extends RemoteServiceServlet implements
MyService {


public String getResult() throws FileNotFoundException {
File file = new File("asdf");
FileReader fr = new FileReader(file);
return "success!";
}
}

The error occurs at MyService:
[ERROR] Line 3: The import java.io.FileNotFoundException cannot be
resolved

Isn't it possible to throw exceptions through an RPC ?

Jason Essington

unread,
Nov 8, 2007, 1:28:40 PM11/8/07
to Google-We...@googlegroups.com
You can throw exceptions, but only exceptions that are defined in the
emulation library, or your own custom exceptions.

Since for the most part the java.io package isn't part of the
emulation library, you can't really return a FileNotFoundException,
or for that matter use FileNotFoundException at all in your client code.

You'll need to wrap that FileNotFound exception on the server into a
custom exception that extends SerializableException, and return that.

-jason

Unknickable

unread,
Nov 9, 2007, 3:01:35 AM11/9/07
to Google Web Toolkit
Thank you!
I tried this before, but i got the same error. Now I figured out, that
the custom exception has to be in the client package, so no import at
the service is necessary.

Unknickable

Reply all
Reply to author
Forward
0 new messages