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

Eclipse merge multi throws when throwing exceptions with same super class

ObjectMapper.readValue method Throws IOException and also its sub classes: JsonParseException and JsonMappingException extended by java.io.IOException extended by com.fasterxml.jackson.core.JsonProcessingException extended…
Ori Marko
  • 56,308
  • 23
  • 131
  • 233
1
vote
1 answer

Why throws keyword cannot handle the exception thrown in field declaration

In the code below, Thread.sleep(3000); written inside the anonymous class instantiation can be handled only using a try-catch block. Why doesn't the throws InterruptedException clause allow the exception to propagate? public static void…
S Kumar
  • 555
  • 7
  • 21
1
vote
2 answers

Exception doesn't need to be thrown to be caught, but IOException does

Why does the following code compile fine, but the method being called does not need to throw Exception? Isn't Exception a checked exception and not an unchecked exception? Please clarify. class App { public static void main(String[] args) { …
Jeremy Levett
  • 939
  • 9
  • 13
1
vote
2 answers

What is the meaning of keyword "throws" in Swift?

What is meaning of "throws" keyword here: This code take so long to be executed and i think "throws" keyword in the image above related: let url = URL(string:"\(APIs.postsImages)\(postImg)") let imgData = try? Data.init(contentsOf:…
Mohamed Hussein
  • 61
  • 1
  • 10
1
vote
1 answer

What kinds of errors does a func throw out in Swift?

I see some methods throw out errors in Apple's Documentation. But I can't find any information about what it throws. Like this methods below. It's in FileManager class. func moveItem(at srcURL: URL, to dstURL: URL) throws I want to know what…
Matt
  • 31
  • 3
1
vote
1 answer

Can I catch a throw exception from method?

When I try to throw an exception from method declaration I get the error "Unreachable catch block for ClassNotFoundException. This exception is never thrown from the try statement body". The code is this: public class MenuSQL { private static…
mindOf_L
  • 911
  • 2
  • 13
  • 23
1
vote
1 answer

do swift throwing functions block main thread?

in swift if a long running throwing function is called from the main thread will I be blocking my UI? Or do throwing functions occur asynchronously automatically? Just wondering if I should dispatch it off the main thread or if that will be…
alionthego
  • 8,508
  • 9
  • 52
  • 125
1
vote
1 answer

CloneNotSupportedException in actionPerformed

In my actionPerformed method i'm calling "copy()" who clones objects, but the compiler gives me this error: "java.awt.event.ActionListener; overridden method does not throw java.lang.CloneNotSupportedException", what can i do? public void…
Trevor
  • 11
  • 1
1
vote
2 answers

Why don't you have to handle possible exceptions thrown by classes in the `java.lang` package?

So, in the documentation for example: java.lang.Integer.parseInt, I noticed that the code header is: public static int parseInt(String s) throws NumberFormatException However, when one has a statement something like int i =…
Jonah Haney
  • 521
  • 3
  • 12
1
vote
2 answers

SONAR issue "main" should not "throw" anything JAVA 7

We have this code. SONAR is complaining about the main() function. "main" should not "throw" anything There's no reason for a main method to throw anything. After all, what's going to catch it? Instead, the method should itself gracefully handle…
saz
  • 291
  • 4
  • 16
1
vote
3 answers

How to use try/catch to avoid throwing FileNotFoundException in java

I am trying to output a file scanner object from my method. This is a school assignment and I am specifically instructed to NOT throw any exceptions, but use try/catch instead. The assignment requires that the command line prompt the user for a…
M. Fleck
  • 13
  • 1
  • 4
1
vote
3 answers

Do we need to throw the same exception in catch block which is already mentioned in method signature using throws declaration

I have a method in my Java App which throws SQLException. Is it necessary to throw the SQLException in the catch block so that the exception is thrown to the calling method where the exception is handled? public void insert(Connection conn) throws…
MN Karthikeyan
  • 61
  • 2
  • 10
1
vote
1 answer

Specifying throws IOException

Can anyone help me solve my problem. I am a beginner in Java programming. Previously when I did not declare throws IOException it gave me an error: Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - unreported…
user4679028
1
vote
1 answer

Given the method signature A bar(B q) throws C, which of the following will not compile?

public class A extends Exception {....} public class B extends A {....} public class C extends RuntimeException {....} Given the method signature A bar(B q) throws C, which of the following will not compile? A. A m() throws C { return bar(new…
sumitz1212
  • 63
  • 4
  • 10
1
vote
3 answers

Throws statement doesn't consider inherited exception

I have 2 exceptions: class MyException1 extends Exception { -- } class MyException2 extends MyException1 { -- } function invokeValidation() throws MyException2 { obj1.method() // it throws MyException1 obj2.method() // it throws…
skumar
  • 985
  • 4
  • 14
  • 37