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
3
votes
2 answers

How to handle a runtime error with throws

In the following code snippet, there are cases where the processes cannot handle NullPointerException and IllegalStateException. Namely in the case where I have the input values val=-4 or val=-2. I read that adding throws after methods' signatures…
Suslik
  • 929
  • 8
  • 28
3
votes
3 answers

What is the cleanest way in swift to throw an optional error?

To unwrap an optional and pass it to a function I normally use: var optionalInt: Int? optionalInt.map { someFunctionThatTakes(aNonOptional: $0) } Now I have an optional error that I would like to throw if it is not nil: var optionalError:…
Andy
  • 4,441
  • 1
  • 19
  • 16
3
votes
1 answer

Signature of Error throwing Function in TypeScript

Is there a "best practice" sort of way how one would mark a function in TypeScript with the information that this function throws an error? In Java one would annotate the function's signature with "throws XYError". This does not work with…
Simeon
  • 748
  • 1
  • 9
  • 26
3
votes
1 answer

Throw exception in Java

Suppose I have a class, the requirement is that "change the following code to throw checked exception if "den" is 0, and change the function to catch that exception". public class divide { void divide(int num, int den){ …
user6119494
  • 65
  • 1
  • 7
3
votes
3 answers

When to catch the exceptions (high level vs. low level)?

I have created the following classes: Abstract logger class Three subclasses of the abstract logger class which implement actual loggers Class which acts as an interface to the logger The three subclasses can throw exceptions (creating file,…
machinery
  • 5,972
  • 12
  • 67
  • 118
3
votes
3 answers

How can I make Eclipse (or javac) warn about over-inclusive throws clauses

I am working on a project (someone else's code) in which a method was declared to throw a bunch of checked exceptions it could not possibly throw. Basically, the method looked like this: // Assume E1 extends Exception // Assume E2 extends…
0xbe5077ed
  • 4,565
  • 6
  • 35
  • 77
3
votes
3 answers

Is there any other role of throws instead of propagating a checked Exception?

After studying more and more about throws statement in Exception Handling I am getting confused.I found- If a method is capable of causing an exception that it does not handle, it must specify this behavior so that callers of the method can…
TheCurious
  • 593
  • 1
  • 4
  • 29
3
votes
2 answers

Throws rule in java

These days, I have been trying to learn java, But I am confused about Throws exceptions. is this statement correct?: Throws in method signature causes an exception (or some exceptions) throw to another method that is calling this method. and caller…
3
votes
1 answer

Testing if JS method throws RangeError with QUnit

I made some Javascript unit tests with QUnit library, then I want to test if method throws RangeError. It has be done in this way: QUnit.test("Resolve slide animation from left", function( assert ) { var myOwnCreator = new…
Radek Anuszewski
  • 1,812
  • 8
  • 34
  • 62
3
votes
2 answers

Is it useful to declare parent and child exceptions in the same throws clause?

I know that the code below does make sense: try { ... } catch (FileNotFoundException exc) { ... } catch (IOException exc) { ... } But does declaring those parent and child exceptions in the throws clause make sense? Suppose I have the following…
MC Emperor
  • 22,334
  • 15
  • 80
  • 130
3
votes
4 answers

throw an exception on a method call

How do I throw and UnsupportedOperationException on a method? So if I have an Iterable object and I'm trying to disallow the remove method for that object. In the method below I'm returning an iterable object whose iterator's remove I need to…
user1766888
  • 417
  • 1
  • 8
  • 21
3
votes
4 answers

Return boolean true if no exception was thrown indicating operation was successful?

Should a method (remote method call) return a boolean true value indicating that an operation performed successfully even if all possible exceptions are thrown? Example: In my java application have many CRUD remote method calls and I catch all…
djmj
  • 5,579
  • 5
  • 54
  • 92
2
votes
3 answers

Why OSGI BundleActivator methods are declared with "throws Exception"?

Both start and stop methods of OSGI BundleActivator are declared with throws Exception. At the same time in his book Effective Java, Second Edition, Item 62, Joshua Bloch says never declare a method "throws Exception" So is it a poor design…
vitaut
  • 49,672
  • 25
  • 199
  • 336
2
votes
3 answers

In java how do I have a class both implements MouseListener and throws IOException?

The throws IOException is for input from a file.
Zane
  • 159
  • 2
  • 5
2
votes
2 answers

Handling orElseThrow

My method looks like this public EP updateEP(long id, EP eP) { EE eE = eRepo.findById(id).orElseThrow(EntityNotFoundException::new); //some code } and my test method looks like this @Test public void…
Uga Rimad
  • 97
  • 6