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

Java - Trying to throw an error based on 2 string values in the parameter

I am trying to validate a parameter based on 2 conditions. must be a string , case insensitive. If the parameter does not equal "title" or "author" it should throw an error. The first part of the code seems to work but the second part is saying its…
kdhillon
  • 13
  • 1
0
votes
0 answers

Why don't I need to declare an exception in a method [Java]?

In the following Code:: public class Main { static void checkAge(int age) { if (age < 18) { throw new ArithmeticException("Access denied - You must be at least 18 years old."); } else { System.out.println("Access granted - You…
Nor.Z
  • 555
  • 1
  • 5
  • 13
0
votes
2 answers

Java throws clause - sonarqube issue

I have kind of old code where we run sonarqube. I am not expert on Java and Exception descendants etc. so I hope someone will be able to help me to fix this issue, as Sonar says it is a blocker. This is the code: package xxx; import…
Martin Fric
  • 726
  • 2
  • 9
  • 27
0
votes
0 answers

exception thrown from constructor

I know how to throw an exception from a constructer and handle it in the main method. but the way I know requires two steps : 1- creat a static object (holder) using the constructer with parameters that take variables. 2-creat an object using the…
Amani
  • 11
  • 1
0
votes
1 answer

"Get around" throws clause by subclassing Runtime Exception

Q: Is there any way to "get around" the strict restrictions placed on methods by the throws clause? According to me we can "get around" throws clause by using try-catch but the answer I found is- A: Yes. Suppose you have thought long and hard and…
Anshul Gupta
  • 265
  • 2
  • 12
0
votes
1 answer

Why throws for IOException in method signature is needed for only specific cases?

public static String doSomething() { String name = "ABC"; try { throw new IOException(); } finally { return name; } } O/p:ABC However below code needs throws in the method signature. Is it because return statement…
learningIsFun
  • 142
  • 1
  • 9
0
votes
3 answers

How to throw an IllegalArgumentException when an array is null or empty?

/** * This constructor accepts an array of points as input. Copy the points into the array points[]. * * @param pts input array of points * @throws IllegalArgumentException if pts == null or pts.length == 0. */ …
0
votes
3 answers

How to handle exceptions in Android by using throw

I am trying to get Information from a website and display it in an Android app. The second answer to the question "What is the fastest way to scrape HTML webpage in Android?" suggested to use BufferedReader. In the answer, the person uses the URL…
MamoRatzo
  • 11
  • 4
0
votes
1 answer

Invalid conversion from throwing function of type '(_, _) throws -> ()' to non-throwing function type '(Bool, Error?) -> Void

I have very simple class which fetches the contacts. Now I need to create this function with throws. As store.requestAccess is not throwing function so I can't throw any error from that clousure. So I am getting this error Invalid conversion from…
Prashant Tukadiya
  • 15,838
  • 4
  • 62
  • 98
0
votes
1 answer

Use "throws" keyword on callback

In my method below, I use a method in the net.sf.extJWNL package in the callback of the .anyMatch() method of Stream. However, the method I am using throws a JWNLException. Instead of the current try-catch block, I would like to use the…
Marvin
  • 853
  • 2
  • 14
  • 38
0
votes
2 answers

Cannot convert value of type '(() throws -> Void?) -> ()' to expected argument type '((Void) throws -> Void)?'

I have a reactive method who should return ((Void) throws -> Void)? to be used into .on(onNext: myUIComponent.rx.function) but no matters what I do I never get what is expected. I was tries func name() throws -> Void? func name(xpto: ((Void) throws…
0
votes
1 answer

when calling void with throws keyword, all thrown errors apply to where i call the void

i am currently trying to make a shift key holder with java using the grave accent as the toggle and robot was the only way i could find that was remotely simple. the program does what the name implies, and holds down the shift key for me. making the…
Vortetty
  • 129
  • 1
  • 1
  • 11
0
votes
0 answers

When and where to use throws instead of a try catch in Java?

I am currently learning Java and found out that there are two ways to deal with checked exceptions. by using try/catch() statements and enclosing the code within them by using throws clause for eg: using try/catch() public static void…
Rahul.In
  • 61
  • 9
0
votes
1 answer

Method overloading and throws exception handling

Let's say I have the following code: public void methodOne(String argumentOne) { methodOne(argumentOne, false); } public void methodOne(String argumentOne, boolean equality) { //App logic here } And if the app logic throws an exception…
Mohan Krishnan
  • 353
  • 3
  • 16
0
votes
2 answers

How to avoid UnsupportedEncodingException in Junit

I have the following code which needs the UnsupportedEncodingException to be handled by try-catch or throws declaration but I want to use neither of them. @Test public void serializeAndDeserializeUTF8StringValueExpectingEqual() { String…
Salem Masoud
  • 411
  • 11
  • 32