[1754] in java-interest

home help back first fref pref prev next nref lref last post

Re: Throwing and catching exceptions...

daemon@ATHENA.MIT.EDU (Arthur van Hoff)
Sat Sep 16 06:03:54 1995

Date: Thu, 14 Sep 1995 15:58:59 -0800 (PDT)
From: Arthur van Hoff <Arthur.Vanhoff@eng.sun.com>
Reply-To: Arthur van Hoff <Arthur.Vanhoff@eng.sun.com>
To: Chuck Williams <chuck.williams@template.com>
Cc: java-interest@java.Eng.Sun.COM



Hi Chuck,

> I've just downloaded the pre-beta JDK and converted an app from the Alpha
> release.  I made what I thought were all the changes I needed and compiled
> which produced these errors:
> 
> classes/IpcTest.java:19: Exception java.io.IOException cannot be thrown in
> initializer.
>             new sun.net.NetworkClient("lion", 1719);
>             ^
> classes/IpcTest.java:62: Warning: Exception java.net.UnknownHostException
> must be caught, or it must be declared in throws clause of this method.
>             client.openServer("lion", 1719);
>                              ^
> 
> "must be caught, or it must be declared in throws clause of this method."
> mean??
> 
> Here are lines 18-20:
> 
>     sun.net.NetworkClient client =
>             new sun.net.NetworkClient("lion", 1719);
> 
> 
> and here are lines 60-62:
> 
>         if (!connected) {
>             client.openServer("lion", 1719);
>             connected = true;
>         }
> 
> Thanks in advance...

Initializers can not throw exceptions. Initializers are effectively inlined
in constructors and they may cause unexpected exceptions to be thrown in
those constructors. The fix is to do the initialzation in a constructor and
have the constructor either catch the exception or declare it.

Static initializers cannot throw any exceptions so you must catch them.

Have fun,

	Arthur van Hoff

-
Note to Sun employees: this is an EXTERNAL mailing list!
Info: send 'help' to java-interest-request@java.sun.com

home help back first fref pref prev next nref lref last post