• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Exception in static block..

 
Ranch Hand
Posts: 52
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What will happen if an exception occurs in a static block///

class A{

static
{

sysout("static block started ");
int a = 0;
int b=500;
int c= 0;
c=b/a;
sysout("static block finished");
}

pvsm(){

sysout("Now i am in main");

}

}

What will be the output ?? ......

Please give explaination..
 
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What happened when you ran it ??

Also, please read this ShowSomeEffort

Using psvm is not cool ;-)
 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Abhineet,

I tried your code which works fine without any exception. Then i changed division statement in static block to c=b/a. This time it gave following output -

static block started
java.lang.ExceptionInInitializerError
Caused by: java.lang.ArithmeticException: / by zero
at test.<clinit>(test.java:9)
Exception in thread "main"

Static block is executed before execution of Main method starts. Any exception E if not caught in static block then If the class of E is not Error or one of its subclasses, then a new instance of the class ExceptionInInitializerError is created, with E as the argument, and used. But if a new instance of ExceptionInInitializerError cannot be created because an OutOfMemoryError occurs, then instead an OutOfMemoryError object in place of E is used.

Regards
 
This tiny ad is wafer thin:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic