Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8878063
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T19:37:20+00:00 2026-06-14T19:37:20+00:00

When I try to throw an exception in an instance initialization (not class initialization)

  • 0

When I try to throw an exception in an instance initialization (not class initialization) block I get the error:

initializer must be able to complete normally

Why is it not allowed although Java does it itself?

The following example creates four classes. The class A fails during instantiation because of an ArithmeticException. This can be and handled with a catch. The same for B which fails with a NullPointerException. But when I try to throw a NullPointerException on my own as in C the program does not compile. And I get the same error when I try to define my own RuntimeException as in D. So:

How can I do the same as Java does itself?

// -*- compile-command: "javac expr.java && java expr"; -*-

class expr
{
    class A
    {
        int y;
        {{ y = 0 / 0; }}
    }

    class B
    {
        Integer x = null;
        int y;
        {{ y = x.intValue(); }}
    }

    class C
    {
        {{ throw new NullPointerException(); }}
    }

    class Rex extends RuntimeException {}

    class D
    {
        {{ throw new Rex(); }}
    }

    void run ()
    {
        try { A a = new A(); }
        catch (Exception e) { System.out.println (e); }

        try { B b = new B(); }
        catch (Exception e) { System.out.println (e); }

        try { C c = new C(); }
        catch (Exception e) { System.out.println (e); }

        try { D d = new D(); }
        catch (Exception e) { System.out.println (e); }
    }

    public static void main (String argv[])
    {
        expr e = new expr();
        e.run();
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T19:37:22+00:00Added an answer on June 14, 2026 at 7:37 pm

    initializer must be able to complete normally

    means that there must be a possible code path that doesn’t throw an exception. Your examples unconditionally throw, and are therefore rejected. In the other examples, the static analysis doesn’t go far enough to determine that they also throw in all cases.

    For example,

    public class StaticThrow {
        static int foo = 0;
        {{ if (Math.sin(3) < 0.5) { throw new ArithmeticException("Heya"); } else { foo = 3; } }}
        public static void main(String[] args) {
            StaticThrow t = new StaticThrow();
            System.out.println(StaticThrow.foo);
        }
    }
    

    compiles, and when run throws

    Exception in thread "main" java.lang.ArithmeticException: Heya
            at StaticThrow.<init>(StaticThrow.java:3)
            at StaticThrow.main(StaticThrow.java:5)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to throw an exception (without using a try catch block) and my
My program compiles fine, but crashes everytime throw (not inside of a try...catch block)
When an exception is thrown or encountered: void ThrowException() { try { throw new
try { // Code } catch (Exception ex) { Logger.Log(Message, ex); throw; } In
int somefunction(bool a) { try { if(a) throw Error(msg); return 2; } catch (Error
I have a class that might throw any run-time exceptions during initialization. I want
For instance, write a function such as: void foo() { try { throw new
I'm trying to throw a format exception in the instance someone tries to enter
when using this method public List<Field> getFieldWithoutId(List<Integer> idSections) throws Exception { try { Connection
It throws exception when i try to combine scripts in ScriptRegistrar (Telerik version 2011.3.1115)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.