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

Declaring checked exception using throws clause that is never thrown in the body of the method

I understand that catching a checked exception that is never thrown in the corresponding try block is invalid. Because the compiler itself would have forced the programmer to handle the exception if it was to occur. For example, this code snippet…
2
votes
2 answers

Thrown checked exception without throws declaration in method

The following code compiles and runs on Java 13: public class CheckedExceptionSSCE { public static void main(String[] args) { try { methodNoThrowsDeclaration(); } catch (Exception e) { // why is this…
Dariusz
  • 21,561
  • 9
  • 74
  • 114
2
votes
7 answers

What's the purpose of the 'throws' statement in Java?

I'm just learning java, but it seems that in the end most of the methods in the beginning of the call stack will just have declarations 'throws Exception'. What's the good thing about this statement that I'm missing that makes it useful? One more…
Max Yankov
  • 12,551
  • 12
  • 67
  • 135
2
votes
4 answers

Java 'throws' keyword in Dart

Im coming from a Java background where I use the throws keyword to lead an exception to the method calling another method. How can I do that I dart? Method called: void _updateCurrentUserEmail() async { await FirebaseAuth.instance …
Patrick
  • 552
  • 5
  • 17
2
votes
3 answers

How to call a method that throws an exception in catch block?

I am trying to have an HandleException method that can handles various exceptions. The problem is, my function returns a value. But if I use HandleException in my catch block, Java complains that the function does not return a value even though my…
SyncMaster
  • 9,754
  • 34
  • 94
  • 137
2
votes
1 answer

Method signature including the throws exception?

I know the method signature is including method name and its parameter list. But how about throws Exception? public List listServiceStatuses() throws RetrieverException { ... return list; } If it's not included then why I…
Hearen
  • 7,420
  • 4
  • 53
  • 63
2
votes
5 answers

Exceptions in method declaration in java

I have the following method to implement for an ArrayList, but I'm not sure how to go about treating the exception. If the ArrayList is empty does it automatically throw that exception or do I need to write something in the method? public T…
gemart
  • 346
  • 1
  • 3
  • 18
2
votes
1 answer

Ember qunit assert.throws does not work

I installed Ember 2.12 and created new project with a component and a test to ensure it throws an error if required attribute is not provided. I can't get this test to pass. dummy-component.hbs {{value}} {{yield}} dummy-component.js import Ember…
SergPro
  • 23
  • 1
  • 4
2
votes
1 answer

Groovy metaclass, mocking a service method that has throws clause

I have an abstract service: abstract class ParentService { abstract Map someMethod() throws NumberFormatException } And another service that extends above class: class ChildService extends ParentService { @Override …
UjjawalG
  • 21
  • 1
2
votes
4 answers

Trouble understanding external code with throws

I'm trying to understand some code written by someone else, and do not understand how 'throws' is used there. Here's the code: package test; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.PrintWriter; import…
Sam
  • 79
  • 6
2
votes
2 answers

Throw new ArgumentException

In my code, how can i make it possible to show a alert box popping out ? i tried to replay throws new argument exception and add in Response.Write("alert('NOT…
Ng Jing
  • 41
  • 7
2
votes
3 answers

How do I fix a Java:26: error: illegal start of type (throws)

I'm new to writing Java code and I'm having a hard time understand how throwing works. I keep getting errors when I compile a portion of my code that has to do with throwing. How do I fix these? My code: class BankAccount { public String name; …
2
votes
2 answers

How to write a statement to check for my user specified exception CustomerNotEnrolledException

I previously wrote an entire successful program composed of 7 classes(Date, Address, Time, Customer, Course, InClassCourse, OnLineCourse), an interface (Invoice) and of course the test class (CustomerTest). The interface Invoice has a method…
2
votes
8 answers

When would you prefer to declare an exception rather than handling it in Java?

I know we can declare the exception for our method if we want it to be handled by the calling method. This will even allow us to do stuff like write to the OutputStream without wrapping the code in try/catch block if the enclosing method throws…
Epitaph
  • 3,128
  • 10
  • 34
  • 43
2
votes
4 answers

Throwing an exception from an anonymous inner class's constructor

How do I add "throws" to an anonymous inner class's constructor? class Foo { public Foo() throws Exception { } } Becasue this doesn't work public static void main(String[] args) { Foo x = new Foo() { @Override public Foo() throws…
Blubber
  • 1,375
  • 1
  • 15
  • 32