
- 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
Can we write any code after throw statement in Java?
No, we can not place any code after throw statement, it leads to compile time error Unreachable Statement.
Throw keyword in Java
- The throw keyword is used to throw an exception manually.
- Whenever it is required to suspend the execution of the functionality based on the user-defined logical error condition, we will use this throw keyword to throw an exception.
- We need to handle these exceptions using try and catch blocks.
Rules to use throw keyword in Java
- The throw keyword must follow Throwable type of object.
- The throw keyword must be used only in the method logic.
- Since it is a transfer statement, we cannot place statements after throw statement. It leads to a compile-time error Unreachable code.
- We can throw user-defined and predefined exceptions using throw keyword.
Example
public class ThrowKeywordDemo { public static void main(String[] args) { try { throw new ArithmeticException(); System.out.println("In try block"); // compile-time error, unreachable statement } catch (Exception e) { System.out.println(e); e.printStackTrace(); } } }
The above code doesn't execute because there is a statement after a throw statement in the try block, it can cause the compile-time error. So we cannot put any statements after a throw statement in Java.
Output
unreachable statement System.out.println("In try block");
- Related Articles
- Can we define a package after the import statement in Java?
- Can we write an interface without any methods in java?
- How do we use throw statement in JavaScript?
- Can we throw an object of generic class in java?
- Can we write any statements between try, catch and finally blocks in Java?
- In MySQL, how we can write Multiple-line statement?
- Can we throw an Unchecked Exception from a static block in java?
- Can constructor throw exceptions in Java?
- Can we re-throw errors in JavaScript? Explain.
- While chaining, can we throw unchecked exception from a checked exception in java?
- Can we write code in try/catch block in JSP as well?
- Can we use Switch statement with Strings in java?
- C++ code to count children who will get ball after each throw
- Can we get the HTTP Response Code in Selenium with Java?
- Can a constructor throw an exception in Java?

Advertisements