Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #30697 > unrolled thread

checked exception thrown from an initializer ok?

Started byAndreas Leitgeb <avl@auth.logic.tuwien.ac.at>
First post2015-01-13 22:40 +0000
Last post2015-01-14 11:11 +0000
Articles 5 — 4 participants

Back to article view | Back to comp.lang.java.programmer


Contents

  checked exception thrown from an initializer ok? Andreas Leitgeb <avl@auth.logic.tuwien.ac.at> - 2015-01-13 22:40 +0000
    Re: checked exception thrown from an initializer ok? Steven Simpson <ss@domain.invalid> - 2015-01-13 22:52 +0000
    Re: checked exception thrown from an initializer ok? Eric Sosman <esosman@comcast-dot-net.invalid> - 2015-01-13 18:06 -0500
    Re: checked exception thrown from an initializer ok? Dmitry Davletbaev <ddomgn@gmail.com> - 2015-01-14 01:17 -0800
      Re: checked exception thrown from an initializer ok? Andreas Leitgeb <avl@auth.logic.tuwien.ac.at> - 2015-01-14 11:11 +0000

#30697 — checked exception thrown from an initializer ok?

FromAndreas Leitgeb <avl@auth.logic.tuwien.ac.at>
Date2015-01-13 22:40 +0000
Subjectchecked exception thrown from an initializer ok?
Message-ID<slrnmbb7o5.uoi.avl@login.logic.tuwien.ac.at>
I just stumbled into a surprise and I can't tell yet, whether
to be relieved or worried...

To avoid some unneccessary verbosity lateron, I defined in a
central class:
  public static final Charset ISO_8859_1 = Charset.forName("ISO-8859-1");

What surprises me is, that eclipse accepts it without barfing
about the unhandled exception.  I'd have expected a compiler 
error, and someone on stackoverflow.com apparently even got
one back in '09, because he asked for aesthetically pleasing
workarounds:
( http://stackoverflow.com/questions/1866770/how-to-handle-a-static-final-field-initializer-that-throws-checked-exception )

JLS for Java 7 - §11.2.3 says:
" It is a compile-time error if a class variable initializer (§8.3.2)
" or static initializer (§8.7) of a named class or interface can throw
" a checked exception class. 

I guess I should be worried, or hope for an alternative interpretation
of this text that didn't yet occur to me...

[toc] | [next] | [standalone]


#30698

FromSteven Simpson <ss@domain.invalid>
Date2015-01-13 22:52 +0000
Message-ID<j6kgob-o96.ln1@s.simpson148.btinternet.com>
In reply to#30697
On 13/01/15 22:40, Andreas Leitgeb wrote:

>    public static final Charset ISO_8859_1 = Charset.forName("ISO-8859-1");
>
> What surprises me is, that eclipse accepts it without barfing
> about the unhandled exception.

> I guess I should be worried, or hope for an alternative interpretation
> of this text that didn't yet occur to me...

The method does not throw any checked exceptions.



-- 
ss at comp dot lancs dot ac dot uk

[toc] | [prev] | [next] | [standalone]


#30700

FromEric Sosman <esosman@comcast-dot-net.invalid>
Date2015-01-13 18:06 -0500
Message-ID<m948cf$3vk$1@dont-email.me>
In reply to#30697
On 1/13/2015 5:40 PM, Andreas Leitgeb wrote:
> I just stumbled into a surprise and I can't tell yet, whether
> to be relieved or worried...
>
> To avoid some unneccessary verbosity lateron, I defined in a
> central class:
>    public static final Charset ISO_8859_1 = Charset.forName("ISO-8859-1");
>
> What surprises me is, that eclipse accepts it without barfing
> about the unhandled exception.  [...]

     It's not necessary to handle unchecked exceptions.  If they
occur, of course, they'll be thrown and you can (if you like)
catch them.  But the compiler doesn't insist that you do so.

-- 
esosman@comcast-dot-net.invalid
"Don't be afraid of work. Make work afraid of you." -- TLM

[toc] | [prev] | [next] | [standalone]


#30704

FromDmitry Davletbaev <ddomgn@gmail.com>
Date2015-01-14 01:17 -0800
Message-ID<bcf8ea63-90dd-4633-92fd-8441f8832dd3@googlegroups.com>
In reply to#30697
On Wednesday, January 14, 2015 at 3:41:20 AM UTC+5, Andreas Leitgeb wrote:
> I just stumbled into a surprise and I can't tell yet, whether
> to be relieved or worried...
> 
> To avoid some unneccessary verbosity lateron, I defined in a
> central class:
>   public static final Charset ISO_8859_1 = Charset.forName("ISO-8859-1");
> 
> What surprises me is, that eclipse accepts it without barfing
> about the unhandled exception.  I'd have expected a compiler 
> error, and someone on stackoverflow.com apparently even got
> one back in '09, because he asked for aesthetically pleasing
> workarounds:
> ( http://stackoverflow.com/questions/1866770/how-to-handle-a-static-final-field-initializer-that-throws-checked-exception )
> 
> JLS for Java 7 - §11.2.3 says:
> " It is a compile-time error if a class variable initializer (§8.3.2)
> " or static initializer (§8.7) of a named class or interface can throw
> " a checked exception class. 
> 
> I guess I should be worried, or hope for an alternative interpretation
> of this text that didn't yet occur to me...

Andreas,

as Steven Simpson already noted there is no checked exceptions. According to Java API, the Charset.forName(String) method throws three exceptions: IllegalCharsetNameException, IllegalArgumentException and UnsupportedCharsetException. All three are subclasses of the RuntimeException which defines unchecked exceptions, i.e. those which can be thrown during the normal operation and need not be caught by "try ... catch" block. That's why compiler doesn't complain about them.

--
Dmitry Davletbaev
Software Developer
dmitry.davletbaev@maginfo.com
http://www.maginfo.com/

[toc] | [prev] | [next] | [standalone]


#30705

FromAndreas Leitgeb <avl@auth.logic.tuwien.ac.at>
Date2015-01-14 11:11 +0000
Message-ID<slrnmbcjnp.uoi.avl@login.logic.tuwien.ac.at>
In reply to#30704
Dmitry Davletbaev <ddomgn@gmail.com> wrote:
> On Wednesday, January 14, 2015 at 3:41:20 AM UTC+5, Andreas Leitgeb wrote:
>> To avoid some unneccessary verbosity lateron, I defined in a
>> central class:
>>   public static final Charset ISO_8859_1 = Charset.forName("ISO-8859-1");
>> JLS for Java 7 - §11.2.3 says:
>> " It is a compile-time error if a class variable initializer (§8.3.2)
>> " or static initializer (§8.7) of a named class or interface can throw
>> " a checked exception class. 
>> I guess I should be worried, or hope for an alternative interpretation
>> of this text that didn't yet occur to me...
>
> Andreas,
> as Steven Simpson already noted there is no checked exceptions.

I could have sworn, that some of the relevant exceptions was a subclass of
IOException...

I had some InputStreamReader, that I originally used with "ISO-8859-1", and
that constructor was declared to throw a checked exception that read like
one of those thrown by Charset.forName - maybe that fooled me.

Ok, now I got it, what fooled me:

The constructor InputStreamReader(java.io.InputStream,java.lang.String)
can throw java.io.UnsupportedEncodingException, which is a *checked* one.

The static method Charset.forName(java.lang.String)
can throw java.nio.charset.UnsupportedCharsetException which is *unchecked*

With charset and encoding being almost synonymous (at least in this context),
I just didn't notice the different name.

Thanks (also to Steve,Eric) for pointing out the uncheckedness of the
relevant exceptions :-)

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.programmer


csiph-web