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

Different behaviour for NullPointerException and IllegalAccessException

I am trying to understand throws clause in JAVA , I wrote the following piece of code: class check { static void demo() { System.out.println("Hello\n"); throw new IllegalAccessException("demo"); } public static void…
0
votes
2 answers

Unit test code in catch statement with a throw

Is there a way to test code in catch statement, the trick is the catch ends with a throw and this is where things go side ways. So the question is,is there a way to avoid that throw when running my Test method or maybe accomodate it on my test? The…
Anele Ngqandu
  • 115
  • 1
  • 17
0
votes
2 answers

Usage of throws command in java

I know questions like this are everywhere, but I read a lot of things about this, and I still can't understand what the "throws" command do. I will be more specific now: So, one of the examples I saw was this one, with the following code: public…
Tricolor
  • 133
  • 3
0
votes
1 answer

Java: How to take the first word in each line from a text file and write it to a new text file?

My prof posted this to practice exception handling and reading and writing to textfiles: Write a method called removeFirstWord removeFirstWord returns an int and takes two strings as arguments. The first argument is the name of the input file and…
LLgood
  • 37
  • 2
  • 8
0
votes
1 answer

"Using try-catch and throws both for exception handling", is it a good approach?

"Using try-catch and throws both for exception handling", is it a good approach ? What are the applicable pros/cons of using this approach ? Please look at the below sample code snippet, I am having a try-catch block as well as throws clause. In…
Avi Tyagi
  • 61
  • 3
0
votes
3 answers

What to do about potential error 'throws' before submitting app to app store?

Lets say we have an app that we are about to submit to the app store... but it contains the following code: try? audioSession.setCategory(AVAudioSessionCategoryAmbient) if error != nil { // this should never happen } …
Nerdy Bunz
  • 6,040
  • 10
  • 41
  • 100
0
votes
1 answer

how do i make asynctask throw WriterException?

When i try to write the code like this error occurs: doInBackground(String...)' in 'com.....QrGenerator' clashes with 'doInBackground(Params...)' in 'android.os.AsyncTask'; overridden method does not throw 'com.google.zxing.WriterException' Here is…
shotofop
  • 11
  • 4
0
votes
1 answer

propagating Exception from inside of a forEach loop

public controllerMethod() throws UnsupportedEncodingException { getVehicles(req); } public List getVehicles(A req) throws UnsupportedEncodingException{ someObject.forEach(obj -> { getVehicles2(req); //try catch resolves but…
Rk R Bairi
  • 1,289
  • 7
  • 15
  • 39
0
votes
1 answer

Place to declare "throws IOException" in GUI

I am new (and German so my English is not the best :D) I am working at a program, but I need to insert a BufferedReader & FileReader. I am working with GUI(graphical user interface) , and I know the mistake that I have to insert a throws…
Jana
  • 25
  • 1
  • 6
0
votes
1 answer

How to execute the @Test after throwing an exception on selenium webdriver

I'm trying to execute @Test outside the throw exception method. The only way I can be able to execute the @Test methods is if I call them within the throws exception method and this makes the @Test methods to fail. Please see my code. public class…
0
votes
3 answers

Can we use generic exception in throws?

I have a silly doubt. While using throws I know we specify multiple exceptions. My query is can we include Exception (generic) also along with other specific exceptions? Is that right coding approach? public String display() throws IOException,…
0
votes
3 answers

Java - throws not working for a subtype of an exception that is handled

Take this as example: public class TestClass { public static void test() throws SSLHandshakeException { throw new SSLHandshakeException("I'm a SSL Exception"); } public static void main(String[] args) throws…
Alex Chihaia
  • 145
  • 2
  • 4
  • 19
0
votes
1 answer

Clarification on throws declaration

I have the following SQL transaction in java public List find() { Session session = sessionFactory.openSession(); Transaction tx = null; List obj = null; try { tx =…
user_mda
  • 18,148
  • 27
  • 82
  • 145
0
votes
2 answers

Why does this do-while loop not function?

I have made a game of battleships using 2D Arrays and need my do-while loop to work so if you place a ship where there already is one, you get asked to place the ship again. Below is the code that changes the array values from 0 to 1 to resemble a…
0
votes
3 answers

Illegal start of expression while throwing an exception?

I keep getting the error Illegal start of expression in this part of the code. switch(length) { case 1: if(message.equalsIgnoreCase("End")){ throws new AnotherException("Stop",true); } else { throws new…