Hello,
The problem is that the URL constructor and the openStream method both
throw Exceptions (not Runtime Exceptions). You are not allowed to throw
non Runtime Exceptions in initializers.
You can, however, initialize the private members in an empty constructor.
There, you can either explicitly throw the two exception, or you can
catch the exceptions and deal with them appropriately.
See below for examples, and good luck,
-AMT
--------------------------------------------------------------------------
Possibility 1:
--------------------------------------------------------------------------
package emailtools;
import java.io.*;
import java.net.*;
...
...
public class EmailBean {
...
...
private URL url = null;
private BufferedReader in = null;
public EmailBean() throws MalformedURLException, IOException {
url = new
URL("http://127.0.0.1:8080/examples/jsp/emailtools/test.jsp");
in = new BufferedReader(new InputStreamReader(url.openStream()));
}
}
--------------------------------------------------------------------------
Possibility 2:
--------------------------------------------------------------------------
package emailtools;
import java.io.*;
import java.net.*;
...
...
public class EmailBean {
...
...
private URL url = null;
private BufferedReader in = null;
public EmailBean() {
try {
url = new
URL("http://127.0.0.1:8080/examples/jsp/emailtools/test.jsp");
in = new BufferedReader(new
InputStreamReader(url.openStream()));
} catch (MalformedURLException e) {
throw new RuntimeException("Provided URL invalid");
} catch (IOException e) {
throw new RuntimeException("Unable to open URL input stream");
}
}
}
--------------------------------------------------------------------------
> -----Original Message-----
> From: A mailing list about Java Server Pages specification and reference
> [mailto:[EMAIL PROTECTED]]On Behalf Of Xunming Liu
> Sent: Thursday, May 11, 2000 2:23 PM
> To: [EMAIL PROTECTED]
> Subject: How I solve this MalformedURLException and IOException
> problems?
>
>
> Hi:
>
> I can not find where the codes are wrong
>
> //*****************************
> package emailtools;
>
> import java.io.*;
> import java.net.*;
> ...
> ...
> public class EmailBean{
> ...
> ...
> private URL url=new
> URL("http://127.0.0.1:8080/examples/jsp/emailtools/test.jsp");
>
> private BufferedReader in=new BufferedReader(new
> InputStreamReader(url.openStream()));
> ....
> .....
> }
> //******************************************
>
> When I complie it, it shows
>
>
> EmailBean.java:25: Exception java.net.MalformedURLException can't be
> thrown
> in initializer.
>
> private URL url= new
> URL("http://127.0.0.1:8080/examples/jsp/mailtools/testOne.jsp");
> ^
>
> EmailBean.java:26: Exception java.io.IOException can't be thrown in
> initializer.
>
> private BufferedReader in=new BufferedReader(new
> InputStreamReader(url.openStream()));
> ^
> 2 errors
>
>
>
> Thanks!
>
> ==================================================================
> =========
> To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff
> JSP-INTEREST".
> Some relevant FAQs on JSP/Servlets can be found at:
>
> http://java.sun.com/products/jsp/faq.html
> http://www.esperanto.org.nz/jsp/jspfaq.html
> http://www.jguru.com/jguru/faq/faqpage.jsp?name=JSP
> http://www.jguru.com/jguru/faq/faqpage.jsp?name=Servlets
>
===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
Some relevant FAQs on JSP/Servlets can be found at:
http://java.sun.com/products/jsp/faq.html
http://www.esperanto.org.nz/jsp/jspfaq.html
http://www.jguru.com/jguru/faq/faqpage.jsp?name=JSP
http://www.jguru.com/jguru/faq/faqpage.jsp?name=Servlets