How Compiler Knows About Checked Exception?

How Compiler Knows About Checked Exception?

A compiler for the Java programming language checks, at compile time, that a program contains handlers for checked exceptions, by analyzing which checked exceptions can result from execution of a method or constructor. For each checked exception which is a possible result, the throws clause for the method (§8.4.

How compiler is knowing that which type of exception is occurring?

When an exception occurs in your program, catch blocks are examined sequentially from top to bottom to find a match between the type of exception occurred and the type of the exception that the catch block is handling.

How checked exceptions are checked at compile time?

2 Answers. Checked exceptions are checked at compile time to ensure you are handling them, either by catching them or declaring the containing method throws the exception. At runtime, there is no distinction between checked and unchecked exceptions: they are treated identically by the JVM.

David Miller
Author

David Miller

David Miller brings 15 years of experience in global economics, personal finance strategy, and market dynamics. He specializes in turning complex economic trends into actionable insights for everyday readers.