

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Can a method throw java.lang.Exception without declaring it in java?
No, for that matter to throw any exception explicitly you need to create an object of that exception and throw it using the throw keyword.
Without creating an object you cannot throw an exception explicitly, you might create a scenario that causes the respective exception.
Example
Following Java program throws a NullPointerException
public class ExceptionExample { public static void main(String[] args) { System.out.println("Hello"); NullPointerException nullPointer = new NullPointerException(); throw nullPointer; } }
Output
Hello Exception in thread "main" java.lang.NullPointerException at MyPackage.ExceptionExample.main(ExceptionExample.java:6)
- Related Questions & Answers
- Is it possible to throw exception without using "throws Exception" in java?
- Can a constructor throw an exception in Java?
- While chaining, can we throw unchecked exception from a checked exception in java?
- How do you throw an Exception without breaking a for loop in java?
- Can we throw an Unchecked Exception from a static block in java?
- Is it possible to create a custom exception in java without extending Exception class?
- Can the abstract methods of an interface throw an exception in java?
- Can the overriding method throw the super-type of the exception thrown by the overridden method in Java?
- Java lang Integer.toHexString() Method with Examples
- Java lang Long.toOctalString() Method with Examples
- While overriding can the subclass choose not to throw an exception in java?
- How to throw an exception from a static block in Java?
- Can constructor throw exceptions in Java?
- If a method in parent class “throws Exception”, can we remove it in overridden method in java?
- How to throw a C++ exception?
Advertisements