Questions tagged [throws]

throws is a Java keyword. It is used in a method definition to declare the Exceptions to be thrown by the method.

Java distinguishes between checked and unchecked exceptions. Unchecked exceptions (RuntimeException, Error, and their subclasses) can be thrown without restriction. But if a method can generate a checked exception, the compiler requires that it either be caught (with a try/catch block) or declared in the method signature with the throws keyword.

https://en.wikibooks.org/wiki/Java_Programming/Keywords/throws https://docs.oracle.com/javase/tutorial/essential/exceptions/declaring.html

249 questions
133
votes
8 answers

Why is "throws Exception" necessary when calling a function?

class throwseg1 { void show() throws Exception { throw new Exception("my.own.Exception"); } void show2() throws Exception // Why throws is necessary here ? { show(); } void show3() throws Exception //…
nr5
  • 4,228
  • 8
  • 42
  • 82
125
votes
10 answers

How to use Java-style throws keyword in C#?

In Java, the throws keyword allows for a method to declare that it will not handle an exception on its own, but rather throw it to the calling method. Is there a similar keyword/attribute in C#? If there is no equivalent, how can you accomplish the…
Louis Rhys
  • 34,517
  • 56
  • 153
  • 221
103
votes
2 answers

What are the differences between throws and rethrows in Swift?

After searching for some references to figure it out, -unfortunately- I could not find useful -and simple- description about understanding the differences between throws and rethrows. It is kind of confusing when try to understand how we should use…
Ahmad F
  • 30,560
  • 17
  • 97
  • 143
95
votes
3 answers

Is there a throws keyword in C# like in Java?

Possible Duplicate: how to use Java-style throws keyword in C#? i have a function where an exception occurs say for example private void functionName() throws Exception { // some code that might throw an exception } thanks!
user677607
93
votes
10 answers

Is there a way to make Runnable's run() throw an exception?

A method I am calling in run() in a class that implements Runnable) is designed to be throwing an exception. But the Java compiler won't let me do that and suggests that I surround it with try/catch. The problem is that by surrounding it with a…
Regex Rookie
  • 10,432
  • 15
  • 54
  • 88
89
votes
7 answers

When to use throws in a Java method declaration?

So I thought I had a good basic understanding of exception-handling in Java, but I was recently reading some code that gave me some confusion and doubts. My main doubt that I want to address here is when should a person use throws in a Java method…
jbranchaud
  • 5,909
  • 9
  • 45
  • 70
83
votes
11 answers

Throws or try-catch

What is the general rule of thumb when deciding whether to add a throws clause to a method or using a try-catch? From what I've read myself, the throws should be used when the caller has broken their end of the contract (passed object) and the…
James P.
  • 19,313
  • 27
  • 97
  • 155
60
votes
8 answers

Exception handling : throw, throws and Throwable

Can any of you explain what the differences are between throw, throws and Throwable and when to use which?
Sumithra
  • 6,587
  • 19
  • 51
  • 50
48
votes
4 answers

Difference between Throws in method signature and Throw Statements in Java

I am trying to make it clear of the difference between Throws in method signature and Throw Statements in Java. Throws in method signature is as following: public void aMethod() throws IOException{ FileReader f = new…
Weishi Z
  • 1,629
  • 5
  • 18
  • 38
46
votes
3 answers

Can I declare that a php function throws an exception?

Can I declare a function in php that throws an exception? For example: public function read($b, $off, $len) throws IOException
shay
  • 1,317
  • 4
  • 23
  • 35
44
votes
1 answer

Either re-interrupt this method or rethrow the "InterruptedException issue in sonar

In one of my method , interrupted exception and execution exception is coming. I put in try catch like this. try{ //my code }catch(InterruptedException|ExecutionException e) Log.error(" logging it"); throw new MonitoringException("it failed"…
Coderrr
  • 441
  • 1
  • 4
  • 4
41
votes
4 answers

Should I declare unchecked exceptions in the throws specification?

I'm aware checked exceptions have to be handled or specified, but unchecked exceptions are optional. If for some reason I can reasonably expect an unchecked exception to occur in a method, should I add it to the throws specification? Or should I…
mafu
  • 31,798
  • 42
  • 154
  • 247
38
votes
5 answers

Error message "unreported exception java.io.IOException; must be caught or declared to be thrown"

Error: filecontent.java:15: unreported exception java.io.IOException; must be caught or declared to be thrown showfile(); ^ filecontent.java:78: unreported exception java.io.IOException; must be caught or …
Akash Patel
  • 381
  • 1
  • 4
  • 5
34
votes
6 answers

Swift throw from closure nested in a function

I have a function that throws an error, in this function I have a inside a closure that I need to throw the error from it's completion handler. Is that possible ? Here is my code so far. enum CalendarEventError: ErrorType { case UnAuthorized …
shannoga
  • 19,649
  • 20
  • 104
  • 169
28
votes
4 answers

Throw and catch an exception, or use instanceof?

I have an exception in a variable (not thrown). What's the best option? Exception exception = someObj.getExcp(); try { throw exception; } catch (ExceptionExample1 e) { e.getSomeCustomViolations(); } catch (ExceptionExample2 e) { …
Ruslan
  • 14,229
  • 8
  • 49
  • 67
1
2 3
16 17