
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
nHow to throw an exception from a static block in Java? n
A static block is a set of statements, which will be executed by the JVM before the execution of the main() method. At the time of class loading if we want to perform any activity we have to define that activity inside a static block because this block executes at the time of class loading.
Throw an exception from a Static Block
- 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. The code can either come in the form of a static block or as a call to a static method for initializing a static data member.
- In both cases, checked exceptions are not allowed by the compiler. When an unchecked exception occurs, it is wrapped by the ExceptionInInitializerError, which is then thrown in the context of the thread that triggered the class loading.
- Trying to throw a checked exception from a static block is also not possible. We can have a try and catch block in a static block where a checked exception may be thrown from the try block but we have to resolve it within the catch block. We can’t propagate it further using a throw keyword.
Example
public class StaticBlockException { static int i, j; static { System.out.println("In the static block"); try { i = 0; j = 10/i; } catch(Exception e){ System.out.println("Exception while initializing" + e.getMessage()); throw new RuntimeException(e.getMessage()); } } public static void main(String args[]) { StaticBlockException sbe = new StaticBlockException(); System.out.println("In the main() method"); System.out.println("Value of i is : "+i); System.out.println("Value of j is : "+ j); } }
Output
In the static block Exception while initializing/ by zero Exception in thread "main" java.lang.ExceptionInInitializerError Caused by: java.lang.RuntimeException: / by zero at StaticBlockException.(StaticBlockException.java:10)
- Related Articles
- Can we throw an Unchecked Exception from a static block in java?
- Can a constructor throw an exception in Java?
- What is the difference between throw e and throw new Exception(e) in catch block in java?
- While chaining, can we throw unchecked exception from a checked exception in java?
- How to throw a C++ exception?
- How do you throw an Exception without breaking a for loop in java?
- Java static block
- A static initialization block in Java
- Can the abstract methods of an interface throw an exception in java?
- Is it possible to throw exception without using "throws Exception" in java?
- How to throw custom exception in Kotlin?
- A non-static initialization block in Java
- How to execute a static block without main method in Java?
- While overriding can the subclass choose not to throw an exception in java?
- How do I manually throw/raise an exception in Python?

Advertisements