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
5
votes
1 answer

How to declare "throws" for `subscript(dynamicMember:)`in @dynamicMemberLookup in swift?

When using @dynamicMemberLookup in swift, subscript cannot declare a "throws". subscript(dynamicMember member: String) -> Any This is OK. subscript(dynamicMember member: String) throws -> Any This will give a compile error.
leavez
  • 2,119
  • 2
  • 27
  • 36
5
votes
1 answer

@Throws has no effect when the target is a property

While taking a look at this question, I noticed that applying @Throws to a get or set use-site has no effect. Additionally, the only valid targets for @Throws are AnnotationTarget.FUNCTION, AnnotationTarget.PROPERTY_GETTER,…
Salem
  • 13,516
  • 4
  • 51
  • 70
5
votes
2 answers

Function throws AND returns optional.. possible to conditionally unwrap in one line?

I am using an SQLite library in which queries return optional values as well as can throw errors. I would like to conditionally unwrap the value, or receive nil if it returns an error. I'm not totally sure how to word this, this code will explain,…
twiz_
  • 1,178
  • 10
  • 16
5
votes
2 answers

What is Go's equivalent to 'throws' clause?

I am just learning Go programming language. I just read about the way of error handling in Go. There is a nice explanation but I have some confusion about it. The typical way of handing error in Go is just returning error as a second value of…
Krupal Shah
  • 8,949
  • 11
  • 57
  • 93
5
votes
5 answers

Java overriding throws clause

I'm learning for OCJP exam by reading the book written by S G Ganesh and Tushar Sharma. At page 346 there is a text that says: What if you try changing the throws clause? There are many ways to change the throws clause in the overriding method,…
Mike
  • 279
  • 2
  • 11
5
votes
1 answer

Understanding QUnit exception testing

While writing QUnit tests I was suprised by the behaviour of 'throws'. Regarding the following code (http://jsfiddle.net/DuYAc/75/), could anyone please answer my questions: function subfunc() { throw "subfunc error"; } function…
user3190756
  • 53
  • 1
  • 3
4
votes
3 answers

Null Pointer in catch block

I am using a try catch block to catch an exception. The console shows that it is throwing a null value. But it is not going to the catch block. try { System.out.println("Exception here "+SomeObject.getValue()); } catch…
Some Java Guy
  • 4,992
  • 19
  • 71
  • 108
4
votes
5 answers

How to handle Exception in Java 8 Stream?

I have a method where I am traversing through a List and creating List. While doing so, I am calling a method(createResult) to will give a Result also throws CustomException which I am wrapping as ResultClassException. But I keep getting an error…
user3407267
  • 1,524
  • 9
  • 30
  • 57
4
votes
2 answers

Java Exceptions: exception myException is never thrown in body of corresponding try statement

I understand the idea of this error. But I guess I don't understand how this works down the call stack. File Main.java: public static void main(String[] args) { try { Function1(); } catch (myException e) { …
aitee
  • 45
  • 1
  • 1
  • 4
4
votes
4 answers

Difference between throwing Exception and throwing a specific Exception such as NullPointerException

I am wondering what is the difference between throwing just Exception and throwing a specific Exception such as NullPointer Exception. To my current knowledge an Exception should be able to catch any type of exception where as using a specific…
L1ghtk3ira
  • 3,021
  • 6
  • 31
  • 70
4
votes
2 answers

Should I avoid throwing Throwable when dealing with a method that throws Throwable?

I've got an @Aspect annotated class that is calling ProceedingJoinPoint#proceed(). This method throws Throwable and thus the class looks something like this: @Aspect @Component public class MyClass{ @Around("@annotation(myAnnotation)") …
João Neves
  • 944
  • 1
  • 13
  • 18
4
votes
2 answers

List all unreported exceptions (including exceptions which super classes of declared exceptions)

Consider the following scenario public class A extends Throwable {} public class B extends A {} public class C { public void f() throws A, B { // Some condition throw new A(); // Some condition …
user93353
  • 13,733
  • 8
  • 60
  • 122
3
votes
4 answers

Java: Throw an exception to another Thread

I have a java code like this: private class Uploader implements Runnable { // ... public void start() { t.start(); } public void run() { try { while(i=in.read()) { output.write(i); // THIS IS A…
Gino Cappelli
  • 55
  • 2
  • 7
3
votes
0 answers

Force android studio compiler to use try-catch when using @Throws - Kotlin

In Java, Android Studio has a feature to avoid calls to methods that throws exception without try/catch blocks and its auto-complete suggests to: Add exception to method signature or Surround with try/catch. But in Kotlin we don't have throws…
Ali
  • 66
  • 1
  • 7
3
votes
2 answers

Does java compiler optimize unreachable exception catch branches?

Why is the code void methodThrowsException() /*throws Exception*/{ try { // throw new Exception(); } catch (Exception e) { throw e; } } well compiled? AFAIK compiler doesn't analyse code for can it throw an exception or…
J.J. Beam
  • 2,612
  • 2
  • 26
  • 55