Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exception in static constructor

I've dug around SO for an answer to this, and the best one I can find so far is here, however that is geared toward instances with static constructors; I'm only using the class statically.

My code:

public static class MailHelper {      private static string mailHost;      static MailHelper() {          var mailSettings = ConfigurationManager.GetSection("MailSettings") as NameValueCollection;         if (null == mailSettings) {             throw new ConfigurationErrorsException("Missing Mail Settings in the configuration file");         }          mailHost = ConfigurationManager.AppSettings["mailHost"];         if (null == mailHost) {             throw new ConfigurationErrorsException("Missing mailHost setting in the configuration file");         }      }      public static void SendMail(MailMessage Message) {         ...     }  }   try {     MailHelper.SendMail(Message); } catch (ConfigurationErrorsException exc) {     ... }  //  ???     MailHelper.SendMail(Message);   . 

So if the static constructor throws an exception the first time it's called, what happens the second time I try to access the static SendMail() method?

PS: Sorry if you don't like Stroustrup's version of K&R brace styling, but don't edit my post just to change the braces to your preferred Allman style. Thanks.

like image 894
James King Avatar asked Jan 19 '11 16:01

James King


People also ask

Can we throw exception in static method?

A static block can throw only a RunTimeException, or there should be a try and catch block to catch a checked exception. A static block occurs when a class is loaded by a class loader.

Can we handle exception in constructor?

Yes, constructors are allowed to throw an exception in Java. A Constructor is a special type of a method that is used to initialize the object and it is used to create an object of a class using the new keyword, where an object is also known as an Instance of a class.

Why static constructor is not allowed?

We know static keyword belongs to a class rather than the object of a class. A constructor is called when an object of a class is created, so no use of the static constructor.

What is exception constructor?

Constructor and Description. Exception() Constructs a new exception with null as its detail message. Exception(String message) Constructs a new exception with the specified detail message.


1 Answers

Once a type initializer has failed once, it is never retried. The type is dead for the lifetime of the AppDomain. (Note that this is true for all type initializers, not just for types with static constructors. A type with static variables with initializer expressions, but no static constructors, can exhibit subtle differences in the timing of the type initializer execution - but it'll still only happen once.)

Demonstration:

using System;  public sealed class Bang {     static Bang()     {         Console.WriteLine("In static constructor");         throw new Exception("Bang!");     }      public static void Foo() {} }  class Test {     static void Main()     {         for (int i = 0; i < 5; i++)         {             try             {                 Bang.Foo();             }             catch (Exception e)             {                 Console.WriteLine(e.GetType().Name);             }         }     } } 

Output:

In static constructor TypeInitializationException TypeInitializationException TypeInitializationException TypeInitializationException TypeInitializationException 

As you can see, the static constructor is only called once.

like image 73
Jon Skeet Avatar answered Sep 19 '22 13:09

Jon Skeet