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

Java 'throws' clause not needed when throwing a new NPE? and why does adding "throws Exception" give compilation errors?

I got curious about the 'throws' clause and wrote the following piece of code (I use Eclipse with Java7). Originally I had started with only blocks 1 and 5 (expecting a compilation error, which did not occur...) and then this led me to writing the…
PLB
  • 881
  • 7
  • 20
0
votes
3 answers

C# throw exception to caller

I have a function that needs to throw an exception, but I wanted it to throw that exception to the line where I called that function: static int retrieveInt() { int a = getInt(); if(a == -1) throw new Exception("Number not found");…
Ricardo Alves
  • 1,071
  • 18
  • 36
0
votes
0 answers

Returning to the main method when a timeout exception is thrown in a method

So I have been doing a bit of looking but couldn't really find anything that answered my question. In my main method, I have a loop of strings that are going to be run into a method where after it will query 10-12 different tables based of off this…
Tapialj
  • 333
  • 2
  • 3
  • 11
0
votes
2 answers

Where do I write the code to handle my exception?

Alright. So I have this class called "PayrollDemo", which uses a class called "Payroll". I have already set up a couple of exception classes that will manage certain exceptions, now I just need to figure out how to throw these exceptions. I figure,…
Will Troll
  • 23
  • 1
  • 1
  • 3
0
votes
4 answers

What is the point in throws clause?

I understand the point of checked exceptions: to remind the developer of errors that you need to be aware of. I also understand the point of not handling some exceptions if you cannot recover from them. But why is it that, should you decide not to…
Lightfire228
  • 546
  • 1
  • 5
  • 17
0
votes
6 answers

How to specify a message on a method "throws" in Java?

Im trying to return a JOptionePane message dialog for each one of the possible throws on my method: public void add_note(String note) throws FileNotFoundException, IOException, InvalidFormatException{ ... content ... } Is there any way to do…
Cristian
  • 199
  • 2
  • 13
0
votes
1 answer

How to write exceptions (class x throws y)

I have code here which runs a short series of tests to see if the last element in the array is the expected number. The code that I have to put in goes where [???] is currently. //The answer must be the shortest possible class…
0
votes
1 answer

How can I add throws IOExcpetion to this code?

This is my main class: public class Table extends java.applet.Applet implements Runnable { public void init() { Balla.addBall(); } } This is the Balla method: public static void addBall()throws IOException { Random…
JWPM77
  • 5
  • 3
0
votes
1 answer

Is my custom exception correctly coded?

I have written the code for the MonthsNumberException, is this code written correctly? I have having big trouble with exceptions, how would I go about creating the exception for the MonthsNameException so that when the user types in a month not on…
user2288575
  • 23
  • 1
  • 1
  • 8
0
votes
3 answers

when is it required to add throws in Java?

in following example, Eclipse does not ask me to add ' throws EmptyStackException' public E pop() { if (stack.isEmpty()) { throw new EmptyStackException(); } ... } However, in following example, 'throws Exception' is required public E…
Zoe
  • 997
  • 4
  • 10
  • 19
0
votes
2 answers

How can I call my custom exception when an SQLException occurs?

Suppose I have the following lines of code, Class.forName(JDBC_DRIVER); dbConnection = DriverManager.getConnection(DB_URL, USER, PASS); Now what I want is that when an SQLException is caught, I want to throw my custom exception from…
Amir
  • 685
  • 3
  • 13
  • 36
0
votes
2 answers

Exceptions works correct but not how its must

The method processExceptions() should call the method BEAN.methodThrowExceptions and handle exceptions: 1.1. if an exception FileSystemException occurs, then log it by calling the method BEAN.log and throw forward 1.2. if an exception…
Predict_it
  • 247
  • 2
  • 4
  • 13
0
votes
3 answers

Unable to Throw an IllegalArgumentException

I need the program to accept 3 test scores and then print their average, but if a score is less than -1 or greater than 100, it should throw an IllegalArgumentException. I can get the average to print out, but when testing -1 or 101, it doesn't…
0
votes
1 answer

Rethrow exception without adding throws to declaration

Is there any way to avoid a need to declare "throws" in method interface in Java? The problem is: I have a very simple method, which throws NPE. It's very simple, and I added all possible checks, and frankly can't imagine how this can happen, so I…
Kurtevich
  • 345
  • 8
  • 18
0
votes
1 answer

throws statement for handled exceptions -- Java

Suppose the following code: public static void somMethod() throws IOException { try { // some code that can throw an IOException and no other checked exceptions } catch (IOException e) { // some stuff here -- no exception thrown in this…
Roam
  • 4,831
  • 9
  • 43
  • 72