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


Groups > comp.lang.java.programmer > #30704

Re: checked exception thrown from an initializer ok?

Newsgroups comp.lang.java.programmer
Date 2015-01-14 01:17 -0800
References <slrnmbb7o5.uoi.avl@login.logic.tuwien.ac.at>
Message-ID <bcf8ea63-90dd-4633-92fd-8441f8832dd3@googlegroups.com> (permalink)
Subject Re: checked exception thrown from an initializer ok?
From Dmitry Davletbaev <ddomgn@gmail.com>

Show all headers | View raw


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/

Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

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

csiph-web