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

Exception Exception is not compatible with throws clause in Server.main(String[])

I'm running the Lip reading code on Eclipse Indigo from the following link : https://github.com/sagioto/LipReading/blob/master/lipreading-core/src/main/java/edu/lipreading/WebFeatureExtractor.java package main.java.edu.lipreading; import…
user3423466
  • 31
  • 1
  • 5
2
votes
1 answer

Adding 'throws' on an extended class?

I'm making a simple application with the Socket API, but have run into a small issue that I haven't found the answer for. The application is supposed to be multi-threaded, so that the main method starts both the server and client threads. public…
filpa
  • 3,651
  • 8
  • 52
  • 91
2
votes
2 answers

IllegalArgumentException is not being caught or missing something

The following code: try { value = parse(myData); } catch (Exception e) { if ( e instanceof IOException|| e instanceof IllegalArgumentException) { logger.debug("illegal argument"); } else { logger.debug("this is printing"); …
Kevin Rave
  • 13,876
  • 35
  • 109
  • 173
2
votes
1 answer

CDI @Inject throws NullPointerException on Websphere 8.5

I've deployed an ear on websphere 8.5. the application is composed of an ejb jar and a webapp ( Spring MVC ). Here is my ejb : @Stateless public class DiscrepanciesServiceImpl implements DiscrepanciesService { @Inject private DiscrepancyDao…
Fabio_M
  • 879
  • 7
  • 21
2
votes
2 answers

whether "throws UncheckedException" makes any sense

I was studying the Exception chapter in java and wondering if it does any good to put some throws UncheckedException after a method signature. I can see that with checked exceptions that kind of a statement is necessary to compile the code and with…
Victor Mukherjee
  • 10,487
  • 16
  • 54
  • 97
1
vote
7 answers

write thrown exception to file

is it possible to write a thrown exception to a file without using try catch blocks? for example public static void method() throws Exception, SQLException{ if(Exception is thrown){ write Exception to out.txt; write SQLException…
Mike
  • 2,299
  • 13
  • 49
  • 71
1
vote
1 answer

Try-Catch vs re throwing an exception?

I have a function fun1(), which calls fun2(), which might throw an Exception : public T fun1(final String abc) throws XYZException { try { fun2() } catch (final Exception e) { throw new XYZException(); } } If we…
Surbhi Jain
  • 369
  • 1
  • 11
1
vote
0 answers

Questions about exception handling in Java specifically the purpose of the throws keyword

I had a question about the throws keyword in the code below. In the method testLoop3() I am indicating that the IndexOutOfBoundsException may be thrown with the throws keyword. But the method testLoop() is identical except it does not show that…
Brian M
  • 11
  • 2
1
vote
1 answer

How to inform in Kotlin that a function passed by parameter can throw an exception?

I need to inform in my class code that the function passed by parameter (convertorCall) can throw an exception. suspend operator fun invoke( convertorCall: () -> T ): T? For example, if this function were as a method of a class I could do…
Pierre Vieira
  • 2,252
  • 4
  • 21
  • 41
1
vote
1 answer

Usage of throws and try-catch in the same method

Can we use throws and try-catch in the same method? public class Main { static void t() throws IllegalAccessException { try{ throw new IllegalAccessException("demo"); } catch (IllegalAccessException e){ System.out.println(e); } } …
MRM
  • 165
  • 6
1
vote
3 answers

Swift: How to deallocate properties when init throw?

There is a memory leak with this example code. The pointer1 and pointer2 allocate before Person inits successfully. If the init function throws an Error. The deinit function will never be executed. So the pointer1 and pointer2 will never be…
Yanni
  • 580
  • 2
  • 6
  • 21
1
vote
1 answer

Can I define multiple custom exceptions in java in a single class file and invoke them via methods?

I'm trying to get into exception handling via custom exceptions. I'm creating the class CustomExceptions and extending Exception as follows: public class CustomExceptions extends Exception{ public CustomExceptions (String s) { super(s); …
Dasphillipbrau
  • 524
  • 2
  • 8
  • 17
1
vote
1 answer

throws Exception refactoring in java

I am changing my code from Implementation 1 : public User getUser(String userid) { User user; try { // some code to get User }catch(InterruptedException e) { throw new CustomException(); } return…
sau123
  • 352
  • 5
  • 18
1
vote
1 answer

Asserting an exception in QUnit

I am working on a unit test that is testing a generated parser code from a grammar. ( generated via PegJS) Im almost finished with most of the cases that are possible. There are 2-3 of them left and they are expected to throw an exception but i…
1
vote
2 answers

Can Java automatically wrap an object and throw as part of throws exception?

I have multiple methods in class; each method has been declared with "throws Exception" option. Whenever an exception occurs I want one object present in this class to be wrapped automatically and create a custom exception object. This newly…
Srinivas P
  • 91
  • 1
  • 4