Results 1 to 7 of 7

Thread: Exception Handling in Singleton

  1. #1
    Join Date
    Dec 2009
    Posts
    67

    Exception Handling in Singleton

    I have a little problem with the Exception Handling in Singleton. Perhaps I can get help from one of you? I have created the source code of the private constructor can throw an exception. I'm getting now in the declaration line of "test" the error message that said to catch the exception or pass. But how does it work? I also tried the initialization in a static block (then try / catch to make), but then I get the error the "test" may not be initialized. So please anyone knowing some solutions about this, help me.!!

  2. #2
    Join Date
    Dec 2008
    Posts
    161

    Re: Exception Handling in Singleton

    You have just mentioned that you are little problem with the Exception Handling in Singleton. If you are trying some coding, then it would be better if you provide your coding.!! Because after checking the coding, it would be easy for us to rectify the errors. Also you said that you have source code of the private constructor which is throwing an exceptions. So no-one can guess what exactly problem is.!! So I insist you to provide the coding for the solution.

  3. #3
    Join Date
    Dec 2009
    Posts
    67

    Re: Exception Handling in Singleton

    I am extremely sorry for wasting your time because of improper query. Here is my class:
    Code:
    public class TestClass ( 
    
    private static final TestClass test = new TestClass (); 
    
    private TestClass () throws Exception (Test 
    
    // Do something here 
    
    ) 
    
    public static TestClass getTestClass () ( 
    
    return test; 
    
    ) 
    
    )
    Hope that you will able to help me.!!

  4. #4
    Join Date
    Dec 2008
    Posts
    161

    Re: Exception Handling in Singleton

    Well, it might go like this, I think but overall not a good idea. In general, the singleton instance during the initial call of getInstance () - initialize method what your problem could in principle solve only once (if getInstance () throws Exception Test "will be declared" with). I could have learned it in detail in recent years, constructors that throw exceptions to avoid if possible and throw the code, the exceptions to outsource to other methods which, in fact, rational treatment (either in class can be done in or in the calling class). What exactly is to happen in your test class in the constructor, which could throw an exception?

  5. #5
    Join Date
    Jan 2006
    Posts
    211

    Re: Exception Handling in Singleton

    Classes are controlling their single objects even now an anti-pattern. This has several reasons: first, it violates the separation of concerns, secondly, a lot of technical writing unit tests, and thirdly, the client code for the Singleton virtually un (unit) testable because it binds to the implementation. The fact that a singleton then a Globle variable is, I can see also at a disadvantage. As an alternative has proven Dependency Injection. If you want not just a container (Pico-, nano-, Spring, WHATEVER ...) to shoot at the class, is a viable workaround to introduce a factory, which controls the lifecycle of the instances to be published.

  6. #6
    Join Date
    Jul 2006
    Posts
    286

    Re: Exception Handling in Singleton

    I think if the designation of class, method and exception that it is in the example is rather an example of didactic nature as a "serious" implementation. And the approach that such patterns also 'try out manually once outside of "convenient" frameworks like Spring, I think is completely welcomed. Unfortunately withhold 99% of the cellular network to be found in the article stated disadvantages and prevent IMHO an objective assessment, implementation now form one. I'm waiting to be weighed against each other on the support of the GoF book in the alternate implementation forms.

  7. #7
    Join Date
    Jul 2006
    Posts
    442

    Re: Exception Handling in Singleton

    I do not think that using a framework like Spring or the programmatic use of some Dependency Injection without Framework necessarily an "alternative implementation of the form" is, especially in light of the fact that Spring beans, after all, if not otherwise explicitly declared, even Singletons are. Especially when I look at Spring, I see in many places more of an enormously powerful framework that is based on "established" (?) Models and architectures, as an "alternative form of implementation." To that extent, I think nothing is wrong with books or articles that give readers once the basic foundations to this - which in my view, belongs to stumble again on a hand-built singleton. Otherwise, you may stumble on the Singletons in Spring.

Similar Threads

  1. Exception Handling in .Net
    By Bruno007 in forum Software Development
    Replies: 5
    Last Post: 25-12-2010, 01:30 AM
  2. Exception handling in PL/SQL
    By Ameeryan in forum Software Development
    Replies: 5
    Last Post: 25-02-2010, 11:04 PM
  3. Exception handling in EJB
    By KennedII in forum Software Development
    Replies: 5
    Last Post: 25-02-2010, 04:40 AM
  4. Problem in handling exception with Regex
    By Gunner 1 in forum Software Development
    Replies: 5
    Last Post: 16-02-2010, 01:56 AM
  5. Exception Handling in Csharp
    By Jesus2 in forum Software Development
    Replies: 3
    Last Post: 11-11-2009, 11:22 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,058,239.31012 seconds with 17 queries