throw is an action that raises an exception right now; throws is a declaration that a checked exception might propagate
Use throw when your code hits a state it can't recover from — input validation, business rule violation
Use throws in the method signature when you delegate responsibility to the caller
A method can declare multiple exceptions with throws, but each throw statement raises exactly one
The compiler enforces throws for checked exceptions only — RuntimeException never needs a declaration
Biggest mistake: thinking throws handles the exception — it only passes the buck
Plain-English First
Imagine you work at a coffee shop. When a customer orders something you can't make — say, a dish from the kitchen — you don't just stand there frozen. You either shout to the back 'I'm passing this order to the chef!' (that's throw — actively handing off the problem right now) or you put a sign above your register saying 'This counter does not handle food orders — see the chef' (that's throws — a public declaration that you might redirect certain problems). One is an action. The other is a warning label.
Every real application breaks at some point. A file isn't where you expected it. A user types letters into a field that only accepts numbers. A payment gateway times out. The question isn't whether errors happen — they will — it's whether your code communicates those failures clearly or just silently crashes and leaves your teammates guessing at 2 AM. That's why Java's exception mechanism exists, and throws and throw are the two keywords that give you precise, intentional control over it.
Before these keywords, error handling was chaotic. Return codes like -1 or null were used to signal failure, but there was no way to force the caller to acknowledge a problem. You could return null from a method and the caller might cheerfully pass it somewhere else, causing a NullPointerException three layers deep with no useful context. Java's checked exception system, powered by throws and throw, forces error contracts to be part of the method signature itself — you can't ignore them.
By the end of this article you'll know exactly when to write throw new SomeException() versus when to annotate a method with throws SomeException. You'll understand the difference between checked and unchecked exceptions and how throws relates to each. You'll be able to design methods that communicate failure clearly, chain exceptions without losing the original cause, and answer the tricky interview questions that trip up even experienced developers.
throw — How to Raise an Exception Right Now
The throw keyword is an imperative action. When Java hits a throw statement, it immediately stops normal execution and begins unwinding the call stack, looking for something that can handle the exception you just raised. Think of it as pulling a fire alarm — the moment you pull it, everything stops and the emergency protocol kicks in.
You always throw an instance of a class that extends Throwable — in practice, that means a subclass of Exception or RuntimeException. You construct the exception object just like any other object, usually passing a descriptive message to the constructor. That message ends up in the stack trace your colleagues (and future you) will read at 3 AM.
The critical thing to understand is that throw is about a specific moment in time: right now, in this method, something has gone wrong that this code cannot and should not recover from. It's a deliberate decision, not an accident. You're saying 'I've validated the situation, this is wrong, and I'm formally raising an error.' This is completely different from an exception that happens because you forgot to null-check something — that's accidental. A throw is intentional and meaningful.
BankAccount.javaJAVA
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
publicclassBankAccount {
privatefinalString accountHolder;
privatedouble balance;
publicBankAccount(String accountHolder, double initialBalance) {
// Guard the constructor — negative starting balance makes no business senseif (initialBalance < 0) {
// throw immediately creates an exception object and hands control// to the nearest matching catch block up the call stackthrownewIllegalArgumentException(
"Initial balance cannot be negative. Received: " + initialBalance
);
}
this.accountHolder = accountHolder;
this.balance = initialBalance;
}
publicvoidwithdraw(double amount) {
// Validate the amount itself firstif (amount <= 0) {
thrownewIllegalArgumentException(
"Withdrawal amount must be positive. Received: " + amount
);
}
// Then validate the business rule — can't overdraw this accountif (amount > balance) {
thrownewIllegalStateException(
"Insufficient funds. Balance: " + balance + ", Requested: " + amount
);
}
balance -= amount;
System.out.println("Withdrew $" + amount + ". New balance: $" + balance);
}
publicstaticvoidmain(String[] args) {
BankAccount account = newBankAccount("Alice", 500.00);
account.withdraw(200.00); // succeedstry {
account.withdraw(400.00); // fails — only $300 left
} catch (IllegalStateException bankingError) {
// We catch the specific type we expect from this operationSystem.out.println("Transaction denied: " + bankingError.getMessage());
}
try {
// This will hit the constructor guardBankAccount badAccount = newBankAccount("Bob", -100);
} catch (IllegalArgumentException setupError) {
System.out.println("Account creation failed: " + setupError.getMessage());
}
}
}
Account creation failed: Initial balance cannot be negative. Received: -100.0
Pro Tip: Choose the Right Exception Type
IllegalArgumentException is for bad input to a method ('you gave me something wrong'). IllegalStateException is for a valid input that doesn't work given the current state ('your input is fine, but the object isn't ready for it'). Picking the right one makes your stack traces self-documenting.
Production Insight
A throw can be caught and swallowed — and that's the danger.
An empty catch block after throw makes debugging impossible.
Rule: never catch without logging or rethrowing.
Key Takeaway
throw is an intentional action.
Use it to signal that code cannot continue.
Always include a meaningful message.
Decide When to Use throw
IfInput is invalid
→
Usethrow new IllegalArgumentException()
IfObject state prevents operation
→
Usethrow new IllegalStateException()
IfBusiness rule violated
→
Usethrow a custom checked exception with cause
IfThird-party dependency fails
→
UseWrap in your own exception with cause
throws — Declaring That a Method Might Escalate a Checked Exception
Where throw is an action, throws is a declaration. It goes in the method signature, after the parameter list, and it's a public contract saying: 'This method might produce a checked exception. If you call me, you must decide what to do about it — catch it or declare that you'll pass it further up.'
This only applies to checked exceptions. Checked exceptions are the ones the compiler actively tracks — they extend Exception but not RuntimeException. Classic examples are IOException, SQLException, and ParseException. If your method calls anything that throws a checked exception and you don't catch it right there, you must add throws to your own signature.
Think of throws as the method's honest résumé. It's telling callers upfront: 'Here's what might go wrong when you hire me for this job.' This is Java's way of making error handling impossible to accidentally ignore — the compiler literally won't let you call a method with a checked exception without acknowledging the possibility of failure. That's a feature, not a limitation. It forces your team to think about error paths at the API design stage, not after a production incident.
Unchecked exceptions (RuntimeException and its subclasses) don't require throws — you can still add it for documentation purposes, but the compiler won't enforce it.
UserDataLoader.javaJAVA
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
publicclassUserDataLoader {
// This method declares throws IOException because FileReader can throw it.// We're not handling it here — we're delegating the decision to whoever calls us.// The caller is in a better position to decide: retry? show a dialog? log and exit?publicStringreadUserProfile(String filePath) throwsIOException {
StringBuilder profileContent = newStringBuilder();
// FileReader throws IOException if the file doesn't exist or can't be opened// We let that propagate — we added it to our throws clause abovetry (BufferedReader reader = newBufferedReader(newFileReader(filePath))) {
String currentLine;
while ((currentLine = reader.readLine()) != null) {
profileContent.append(currentLine).append("\n");
}
}
// No catch block — IOException will automatically propagate to our callerreturn profileContent.toString();
}
// This method declares TWO possible checked exceptions.// A method can throw multiple exception types, separated by commas.publicDateparseUserBirthdate(String rawDateString) throwsParseException, IllegalArgumentException {
if (rawDateString == null || rawDateString.isBlank()) {
// IllegalArgumentException is unchecked, so it doesn't NEED to be in throws,// but we include it here to document the contract explicitlythrownewIllegalArgumentException("Birthdate string must not be null or blank");
}
SimpleDateFormat expectedFormat = newSimpleDateFormat("yyyy-MM-dd");
expectedFormat.setLenient(false); // "2023-02-31" should fail, not silently roll over// parse() throws ParseException if the string doesn't match — checked, so it propagatesreturn expectedFormat.parse(rawDateString);
}
publicstaticvoidmain(String[] args) {
UserDataLoader loader = newUserDataLoader();
// --- Calling a method with throws IOException ---// The compiler FORCES us to handle it. We can't just call loader.readUserProfile() and move on.try {
String profile = loader.readUserProfile("user_profile.txt");
System.out.println("Profile loaded:");
System.out.println(profile);
} catch (IOException fileError) {
// In a real app you'd log this with a proper logger (SLF4J, Log4j, etc.)System.out.println("Could not load profile: " + fileError.getMessage());
}
// --- Calling a method that throws ParseException ---try {
Date birthdate = loader.parseUserBirthdate("1990-07-15");
System.out.println("Parsed birthdate: " + birthdate);
// Now try a badly formatted dateDate badDate = loader.parseUserBirthdate("15/07/1990");
} catch (ParseException formatError) {
System.out.println("Date format was wrong: " + formatError.getMessage());
}
}
}
Output
Could not load profile: user_profile.txt (No such file or directory)
Parsed birthdate: Sun Jul 15 00:00:00 UTC 1990
Date format was wrong: Unparseable date: "15/07/1990"
Key Insight: throws Is a Contract, Not a Catch
Adding throws to your method signature does NOT handle the exception — it explicitly promises NOT to handle it here, delegating that responsibility to the caller. Many beginners add throws thinking it makes the error 'safe'. It doesn't. It just moves the responsibility up the call stack.
Production Insight
Overusing throws Exception makes your API useless.
Callers can't differentiate between file-not-found and parse failures.
Rule: always declare the most specific checked exceptions.
Key Takeaway
throws is a declaration of intent.
It tells the caller: 'You must handle this.'
Specific exception types lead to better error handling.
Decide When to Add throws
IfCalled method declares a checked exception and you can't handle it meaningfully
→
UseAdd throws with same exception type
IfYou can handle the exception here (log, retry, fallback)
→
UseUse try-catch, don't add throws
IfYou're writing an interface and implementations may throw
→
UseAdd throws to the interface method, implementors can choose to throw or not
throw and throws Working Together — Exception Chaining in Real APIs
Here's where things get powerful. In real-world code, you'll constantly use throw and throws together. A method declares throws in its signature (the contract), and internally uses throw to either re-throw a caught exception or wrap it in a higher-level exception with more context.
Exception chaining is the pattern of catching a low-level exception and wrapping it in a higher-level, more meaningful one while preserving the original cause. You do this with the Throwable cause parameter that most exception constructors accept. Without it, you lose the original stack trace and debugging becomes a nightmare.
The classic real-world scenario: your data layer catches a SQLException, but your service layer shouldn't know or care about SQL. So you catch the SQL exception, throw a new DataAccessException (your own custom exception), but pass the original SQLException as the cause. The caller gets a meaningful error at their level of abstraction, and a developer debugging the issue can still drill down to the exact SQL error that triggered it. This is the difference between a junior developer's error handling and a senior's.
UserRepository.javaJAVA
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
// Custom checked exception — this is what the service layer will see.// It's at the right level of abstraction: 'something went wrong with data access'.classDataAccessExceptionextendsException {
publicDataAccessException(String message, Throwable cause) {
// Passing 'cause' to super() is exception chaining.// The original exception is preserved and accessible via getCause().super(message, cause);
}
}
publicclassUserRepository {
private static final String DB_URL = "jdbc:mysql://localhost:3306/appdb";// This method's throws clause uses OUR abstraction (DataAccessException),// not the low-level SQL one. Callers don't need to import java.sql.*publicStringfindUsernameById(int userId) throwsDataAccessException {
String query = "SELECT username FROM users WHERE id = ?";
try (
Connection dbConnection = DriverManager.getConnection(DB_URL, "appuser", "secret");
PreparedStatement statement = dbConnection.prepareStatement(query)
) {
statement.setInt(1, userId);
ResultSet results = statement.executeQuery();
if (results.next()) {
return results.getString("username");
} else {
// Using throw with a custom message for a business-level 'not found' situationthrownewDataAccessException(
"No user found with ID: " + userId, null
);
}
} catch (SQLException databaseError) {
// We CATCH the low-level SQLException, then THROW our own exception.// The original databaseError is passed as the cause — we don't lose it.thrownewDataAccessException(
"Database error while looking up user ID: " + userId,
databaseError // <-- This is the chaining part
);
}
}
// The service layer calls us and handles DataAccessException.// It never needs to know about JDBC or SQL at all.publicstaticvoidmain(String[] args) {
UserRepository repository = newUserRepository();
try {
String username = repository.findUsernameById(42);
System.out.println("Found user: " + username);
} catch (DataAccessException serviceError) {
System.out.println("Service error: " + serviceError.getMessage());
// getCause() gives us the original low-level exception for debuggingif (serviceError.getCause() != null) {
System.out.println("Root cause: " + serviceError.getCause().getMessage());
}
}
}
}
Output
Service error: Database error while looking up user ID: 42
Root cause: Communications link failure — The last packet sent successfully to the server was 0 milliseconds ago.
Watch Out: Never Swallow the Cause
If you write 'throw new DataAccessException("DB error", null)' instead of passing the original exception, you've just destroyed the root cause. The stack trace stops at your exception and everyone debugging the issue has to guess what actually went wrong. Always pass the original exception as the cause parameter when wrapping.
Production Insight
Losing the cause during wrapping is the #1 debugging time sink.
Without it, you see 'Data access error' and have no idea why.
Rule: always pass the original exception as the second argument.
Key Takeaway
Exception chaining preserves the full failure story.
Wrap low-level exceptions in meaningful abstractions.
Never drop the cause — it's the debugger's only map.
Chaining or Not?
IfYou need to hide implementation details from the caller
→
UseWrap in a custom exception with cause
IfYou want to add context to a low-level failure
→
UseThrow new MyException("context", originalCause)
IfThe exception is a programming bug (e.g., null pointer)
→
UseDon't wrap — let it propagate as unchecked
Common Mistakes That Bite Intermediate Developers
Even developers who understand the basic syntax of throw and throws routinely fall into a handful of traps. These mistakes often don't cause compile errors — they cause subtle runtime bugs or unreadable stack traces that waste hours of debugging time.
The most dangerous mistake is catching an exception and then throwing a new one without preserving the original cause, which we covered above. But there are others that specifically relate to how throws interacts with inheritance, and how throw interacts with finally blocks.
Knowing these patterns separates developers who understand exception handling conceptually from those who just know the syntax.
ExceptionMistakesDemo.javaJAVA
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
publicclassExceptionMistakesDemo {
// ─────────────────────────────────────────────────────// MISTAKE 1: Throwing inside finally — swallows the original exception// ─────────────────────────────────────────────────────publicstaticvoidriskyOperation() throwsException {
try {
System.out.println("Attempting risky operation...");
thrownewException("Something went wrong in the try block");
} finally {
// BAD: If finally also throws, the original exception from try is SILENTLY LOST.// The caller only sees 'Cleanup failed', never knowing about the first error.// throw new RuntimeException("Cleanup failed"); // <-- DON'T do this// GOOD: Do cleanup, but don't throw from finally unless you're certain// the cleanup exception is more important than the original.System.out.println("Cleanup complete (no throw from finally)");
}
}
// ─────────────────────────────────────────────────────// MISTAKE 2: Catching Exception (or Throwable) too broadly// ─────────────────────────────────────────────────────publicstaticvoidbroadCatchAntiPattern(String input) {
try {
int parsed = Integer.parseInt(input); // throws NumberFormatException
int result = 100 / parsed; // throws ArithmeticException if input is "0"System.out.println("Result: " + result);
} catch (Exception everythingCatch) {
// BAD: We have no idea WHICH exception happened.// NumberFormatException needs a different response than ArithmeticException.System.out.println("Something failed: " + everythingCatch.getMessage());
}
// GOOD: Catch specifically, handle meaningfullytry {
int parsed = Integer.parseInt(input);
int result = 100 / parsed;
System.out.println("Result: " + result);
} catch (NumberFormatException inputError) {
System.out.println("Input is not a valid number: " + input);
} catch (ArithmeticException mathError) {
System.out.println("Cannot divide by zero — input must not be 0");
}
}
// ─────────────────────────────────────────────────────// MISTAKE 3: Using throws with unchecked exceptions unnecessarily// and confusing it with actually handling them// ─────────────────────────────────────────────────────// This compiles and runs fine, but the throws NullPointerException is noise.// NullPointerException is unchecked — declaring it in throws doesn't make callers// handle it, it just clutters your method signature.
public static StringgetUpperCase(String text) throws NullPointerException { // <-- noisy
return text.toUpperCase(); // will throw NPE if text is null anyway
}
// GOOD: For unchecked exceptions, validate defensively insteadpublicstaticStringgetUpperCaseSafe(String text) {
if (text == null) {
throw new IllegalArgumentException("text must not be null"); // intentional, meaningful
}
return text.toUpperCase();
}
publicstaticvoidmain(String[] args) throwsException {
riskyOperation();
System.out.println("\n--- Broad catch demo ---");
broadCatchAntiPattern("abc"); // NumberFormatException pathbroadCatchAntiPattern("0"); // ArithmeticException pathbroadCatchAntiPattern("5"); // success pathSystem.out.println("\n--- Safe null handling demo ---");
System.out.println(getUpperCaseSafe("hello"));
try {
getUpperCaseSafe(null);
} catch (IllegalArgumentException validationError) {
System.out.println("Caught expected error: " + validationError.getMessage());
}
}
}
Output
Attempting risky operation...
Cleanup complete (no throw from finally)
--- Broad catch demo ---
Input is not a valid number: abc
Cannot divide by zero — input must not be 0
Result: 20
--- Safe null handling demo ---
HELLO
Caught expected error: text must not be null
Watch Out: throws on main() Is a Shortcut, Not Good Practice
Writing 'public static void main(String[] args) throws Exception' is fine for demos and quick scripts, but in production code main() should have a proper try-catch with real error reporting — logging, exit codes, user-facing messages. Letting exceptions bubble out of main() gives users a raw stack trace, which is both confusing and a potential security exposure.
Production Insight
throws Exception on main() is a ticking time bomb.
The exception goes to stderr, not your logging system.
Rule: always catch and log in main() with a real exit code.
Key Takeaway
Common mistakes are silent killers.
Catch specifically, never throw in finally, and guard main().
Each mistake has a simple fix — apply them before they hit production.
Fix Common Mistakes
IfException thrown in finally block
→
UseUse try-catch in finally, log the cleanup error, don't rethrow
IfCatch block is too broad (catch Exception)
→
UseCatch specific types, handle each differently
IfUnchecked exception in throws clause
→
UseRemove it — it's noise; validate inputs instead
Exception Design Patterns for Robust APIs
Beyond syntax, senior engineers use exception design patterns to make their APIs predictable and debuggable. The three most important patterns are: the 'fail-fast' pattern with throw, the 'abstraction boundary' pattern with throws, and the 'recovery-oriented' pattern with custom exception hierarchies.
Fail-fast means validating inputs at the earliest point — throw an IllegalArgumentException in the constructor or method entry. This prevents corrupted state from propagating. The abstraction boundary pattern uses throws to hide implementation details — your service layer throws ServiceException, not SQLException. The recovery-oriented pattern defines exception subclasses that tell the caller what action to take: RetryableException, NonRetryableException, ResourceNotFoundException.
These patterns reduce cognitive load for callers and make your APIs self-documenting. A well-designed exception hierarchy can cut debugging time by half because the exception type itself tells you what went wrong and what to do next.
Mental Model: Exception Types as Action Instructions
RetryableException → caller applies backoff and retries
ResourceNotFoundException → caller returns 404 to client
NonRetryableException → caller alerts and does not retry
Generic ServiceException → fallback for unknown failures
Production Insight
Generic exceptions force callers to parse messages for action.
That's fragile and error-prone — message changes break logic.
Rule: use exception subclasses to encode the action required.
Key Takeaway
Exception design patterns make APIs self-documenting.
Fail fast, abstract boundaries, encode recovery actions in types.
A good exception hierarchy is worth a thousand comments.
Design Your Exception Hierarchy
IfYou need to indicate that an operation can be retried
→
UseCreate RetryableException subclass
IfYou need to indicate resource not found
→
UseCreate ResourceNotFoundException subclass
IfYou need to indicate a permanent failure
→
UseCreate NonRetryableException subclass
IfYou need a catch-all for unknown failures
→
UseKeep a generic ServiceException as parent
● Production incidentPOST-MORTEMseverity: high
Silent Data Loss: The throws Main Anti-Pattern
Symptom
Users reported missing orders. Logs showed nothing unusual. The application appeared healthy, but a critical batch job had silently failed.
Assumption
The team assumed throws Exception on main() was harmless in a scheduled job — the scheduler would capture any error.
Root cause
A database connection timeout threw a SQLException. The main method had throws Exception, so the JVM printed the stack trace to stderr, which wasn't captured by the logging framework. The scheduler saw a non-zero exit code but didn't report it because the exit code was misinterpreted. The batch job stopped processing, but no alert fired.
Fix
Replace throws Exception in main() with a try-catch that logs with a proper framework (SLF4J) and sets a clear exit code. Add a health check endpoint to monitor job completion.
Key lesson
Never let exceptions escape main() in production — always catch and log with structured logging.
throws in main() is a shortcut for demos, not production code.
A silent exception is worse than a crash — at least a crash triggers an alert.
Production debug guideTrace the flow from throw to catch — and find where exceptions get lost4 entries
Symptom · 01
Exception message appears but no useful stack trace
→
Fix
Check if the exception was wrapped without passing the cause. Look for throw new MyException("msg") without the original exception as the second argument.
Symptom · 02
Exception disappears entirely — no log entry
→
Fix
Look for empty catch blocks (catch (Exception e) {} ) or finally blocks that throw a second exception, swallowing the first.
Symptom · 03
Caller sees a generic Exception with no detail
→
Fix
Inspect the throws clause: if the method declares throws Exception, any checked exception gets masked. Prefer specific exception types in throws.
Symptom · 04
Stack trace shows 'Suppressed: ...' lines
→
Fix
This happens when multiple exceptions occur (e.g., try-with-resources). Use getSuppressed() to access them. Ensure finally blocks don't throw.
★ Quick Reference: Exception Debugging CommandsUse these JVM flags and tools to capture full exception details in production
Exception stack traces are truncated or missing−
Immediate action
Add JVM flag -XX:+PrintStackTraceOnThrow to print stack traces for all exceptions, even caught ones.
Commands
-XX:+PrintStackTraceOnThrow
jcmd <pid> VM.print_exception_statistics
Fix now
Enable -XX:+PrintStackTraceOnThrow in your production JVM args temporarily (low overhead).
Wrapped exceptions lose original cause+
Immediate action
Search for 'throw new' without a cause parameter. The pattern 'throw new X(e)' is correct; 'throw new X()' is the bug.
Commands
grep -r 'throw new.*Exception()' src/
Use static analysis: ErrorProne's 'CatchAndPrintStackTrace' check
Fix now
Add the original exception as cause: throw new MyException("msg", originalException);
Checked exception not declared in method signature+
Immediate action
Compile error: 'Unhandled exception type XXX'. Add throws clause or wrap in unchecked exception.
Commands
javac -Xlint:unchecked
IDE quick fix: 'Add throws declaration'
Fix now
Decide: if caller should handle it, add throws. If it's a programming error, wrap in RuntimeException.
throw vs throws: Quick Comparison
Aspect
throw
throws
What it is
A statement (an action)
A keyword in a method signature (a declaration)
Where it appears
Inside the method body
After the parameter list, before the method body
What it does
Actually raises an exception instance right now
Declares that the method might propagate a checked exception
Applies to unchecked exceptions?
Yes — you can throw any Throwable subclass
Not required for unchecked (RuntimeException). Optional for documentation.
Compiler enforcement
Compiler ensures a Throwable is thrown, not a primitive
Compiler forces callers to either catch or re-declare the exception
Can list multiple?
No — one throw per statement
Yes — throws IOException, SQLException is valid
Relation to catch
Triggers the catch block search immediately
Has no effect on catch — it just documents propagation intent
Inheritance rule
N/A
An overriding method cannot throw broader checked exceptions than the parent method
Used for custom exceptions?
Yes — throw new MyCustomException()
Yes — declare throws MyCustomException in the signature
Key takeaways
1
throw is an action inside a method body that immediately raises an exception
it's a deliberate, intentional signal that something has gone wrong that this code cannot recover from.
2
throws is a contract in the method signature that tells callers 'I might produce this checked exception
you must decide whether to catch it here or pass it further up.' It delegates responsibility, it doesn't handle anything.
3
Always pass the original exception as the cause when wrapping
'throw new HighLevelException(message, originalException)'. Dropping the cause is one of the most common ways senior developers lose hours of debugging time.
4
throws is only enforced by the compiler for checked exceptions (subclasses of Exception that are not RuntimeException). Unchecked exceptions (RuntimeException and its subclasses) propagate freely without any declaration, which is why defensive validation with throw new IllegalArgumentException() is preferred over relying on NullPointerException to surface bugs.
5
Design your exception hierarchy to encode the recovery action
RetryableException, NonRetryableException, ResourceNotFoundException. This makes APIs self-documenting and cuts debugging time.
Common mistakes to avoid
4 patterns
×
Throwing an exception from a finally block
Symptom
The original exception from the try block is silently lost. Only the finally exception appears in logs, making it impossible to know what went wrong first.
Fix
Never throw from finally. If cleanup code can fail, wrap it in its own try-catch inside the finally block and log the cleanup failure separately. Use try-with-resources instead of manual close() when possible.
×
Catching an exception and re-throwing without preserving the cause
Symptom
Stack trace shows your high-level exception (e.g., 'Database error') but no indication of the underlying SQL error. Debugging takes twice as long.
Fix
Always pass the caught exception as the cause parameter: throw new DataAccessException("DB failed", e). Then getCause() and chained stack traces remain available.
×
Adding throws Exception to main() as a shortcut in production
Symptom
Unexpected exceptions in main() are printed to stderr, not captured by the logging framework. The application exits silently, and no alert is triggered.
Fix
In main(), catch all exceptions with a proper handler that logs via SLF4J/Logback and calls System.exit() with a non-zero code. Only use throws Exception for demos or scripts.
×
Using throws with unchecked exceptions like NullPointerException
Symptom
Cluttered method signatures that imply the caller must handle an exception that the compiler doesn't enforce. No benefit, just noise.
Fix
Remove unchecked exceptions from throws clauses. Instead, validate inputs defensively and throw IllegalArgumentException or custom unchecked exceptions.
INTERVIEW PREP · PRACTICE MODE
Interview Questions on This Topic
Q01JUNIOR
What is the difference between throw and throws in Java, and can you giv...
Q02SENIOR
Can you override a method and declare it as throws Exception if the pare...
Q03SENIOR
If a try block throws an exception and the finally block also throws an ...
Q04SENIOR
Design a custom exception hierarchy for a payment processing system. Wha...
Q01 of 04JUNIOR
What is the difference between throw and throws in Java, and can you give a scenario where you'd use both in the same method?
ANSWER
throw is a statement that actually raises an exception. throws is a declaration in the method signature that says the method might propagate a checked exception. You use both in the same method when you call a method that throws a checked exception (e.g., IOException), catch it, and then throw a custom exception of your own (e.g., ServiceException). The method signature would declare throws ServiceException, and inside the method you write throw new ServiceException("context", originalException).
Q02 of 04SENIOR
Can you override a method and declare it as throws Exception if the parent method only declares throws IOException? Why or why not?
ANSWER
No, you cannot. An overriding method cannot throw a broader checked exception than the parent method. IOException is a subclass of Exception, so declaring throws Exception is broader. If you try, you'll get a compile error. The rule exists because polymorphism — a caller expecting the parent type's contract (throws IOException) should not have to handle a broader exception (Exception). An overriding method can throw a narrower (subclass) exception or no checked exception at all.
Q03 of 04SENIOR
If a try block throws an exception and the finally block also throws an exception, what happens? Which exception does the caller receive, and how would you preserve both?
ANSWER
The caller receives the exception thrown in the finally block. The original exception from the try block is suppressed — it is added to the suppressed array of the finally exception. You can retrieve it via getSuppressed(). To preserve both, you should either avoid throwing in finally (preferred), or catch the finally exception and add the original as suppressed: e.addSuppressed(tryException). If you're using try-with-resources, resources' close() exceptions are automatically suppressed.
Q04 of 04SENIOR
Design a custom exception hierarchy for a payment processing system. What base class, subclasses, and exception types would you create?
ANSWER
Base class: PaymentException extends Exception. Subclasses: InsufficientFundsException (caller can handle by offering alternative payment), PaymentGatewayTimeoutException (retryable), FraudDetectionException (non-retryable), InvalidCardException (non-retryable). Also define a RecoveryHint annotation or enum to document retry policy. The hierarchy allows callers to catch specific types and decide the recovery action without parsing messages.
01
What is the difference between throw and throws in Java, and can you give a scenario where you'd use both in the same method?
JUNIOR
02
Can you override a method and declare it as throws Exception if the parent method only declares throws IOException? Why or why not?
SENIOR
03
If a try block throws an exception and the finally block also throws an exception, what happens? Which exception does the caller receive, and how would you preserve both?
SENIOR
04
Design a custom exception hierarchy for a payment processing system. What base class, subclasses, and exception types would you create?
SENIOR
FAQ · 5 QUESTIONS
Frequently Asked Questions
01
Can a method have throws without ever actually throwing the exception?
Yes, and it's more common than you'd think. If your method calls another method that declares throws IOException, you can either catch it or declare throws IOException yourself — even if in the current implementation it never triggers. This is valid and sometimes useful during development or when designing interfaces where future implementations might throw.
Was this helpful?
02
Do I need throws for NullPointerException or ArrayIndexOutOfBoundsException?
No. Both are subclasses of RuntimeException, making them unchecked exceptions. The compiler doesn't require you to declare or catch them. You can add them to a throws clause for documentation, but it has no effect on how the compiler treats callers. The better approach for null inputs is to validate with a throw new IllegalArgumentException() rather than letting a NullPointerException surface.
Was this helpful?
03
Can a constructor use throws?
Absolutely. If a constructor does anything that might throw a checked exception — opening a file, parsing a date, connecting to a resource — it can declare throws just like a method. The caller must then handle it when using 'new MyClass()'. This is a common pattern for resource-heavy objects where construction itself can meaningfully fail.
Was this helpful?
04
What's the difference between throws and throw in Java?
throw is a statement that actually raises an exception (e.g., throw new IOException()). throws is a declaration in the method signature that indicates the method might propagate a checked exception (e.g., void readFile() throws IOException). They work together: a method that throws must either catch the exception or declare it with throws.
Was this helpful?
05
Can I throw multiple exceptions in a single throw statement?
No. One throw statement raises exactly one exception object. To indicate that a method might throw multiple types, you list them separated by commas in the throws clause (e.g., throws IOException, SQLException). Each type is potential; any given call will throw at most one.