Questions tagged [checked-exceptions]

The exceptions that need to be declared in a method or constructor's `throws` clause if they can be thrown by the execution of the method or constructor and propagate outside the method or constructor boundary.

The class Exception and its subclasses are a form of Throwable that indicates conditions that a reasonable application might want to catch.

The class Exception and any subclasses that are not also subclasses of RuntimeException are checked exceptions. Checked exceptions need to be declared in a method or constructor's throws clause if they can be thrown by the execution of the method or constructor and propagate outside the method or constructor boundary.

Details: http://docs.oracle.com/javase/7/docs/api/java/lang/Exception.html

145 questions
1
vote
4 answers

Why try/catch around throwable?

In trying to refactor some I code I attempted to throw the exception in the catch clause like so - try { .... } catch(Exception exception){ ..... throw exception } However when I attempted to throw the exception on line "throw exception" the…
blue-sky
  • 51,962
  • 152
  • 427
  • 752
1
vote
1 answer

How to throw Checked Exception inside lambda Java8?

I am trying to throw an exception inside lambda but it keeps giving me an error saying that Unhandled IOException. private T getResponse(final RestURI query, final Class responseClass) throws IOException { return getValue(query, …
user3407267
  • 1,524
  • 9
  • 30
  • 57
1
vote
0 answers

How to throw a checked exception within CompletableFuture?

I used a static method supplyAsync() to get a CompletableFuture, and call its whenComplete() method to deal with the result and exception. whenComplete() accepts a BiConsumer as parameter, and BiConsumer's second parameter is a Throwable. But I…
1
vote
2 answers

create checked exception class in java

I am puzzled about creating checked exception class in java. Many article says custom exception can be created by class MyException extends Exception { //constructor defined } Since RuntimeException is also inherited from Exception class. Is it…
Ankit
  • 2,126
  • 4
  • 33
  • 53
1
vote
3 answers

JAVA. I am getting an 'unreported exception' compiler error

I am trying to compile this code, but it keeps having an error, errThrower.java:37: error: unreported exception Exception; must be caught or declared to be thrown throw new Exception(); This exception is thrown in the callmethodErr(), and I…
Talia
  • 21
  • 3
1
vote
2 answers

Where do Java exceptions came from?

Until now, I thought that every Java exception has to be created somewhere by a constructor, as I can create on my own, custom exceptions: throw new Exception(); But now it seems that I have to handle some exception from JavaMail -…
Line
  • 1,529
  • 3
  • 18
  • 42
1
vote
3 answers

What's wrong with throwing multiple checked exceptions in Java?

Consider the Java code snippet below: public T create(Class clazz) throws InstantiationException, IllegalAccessException { return clazz.newInstance(); } This in Eclipse (Neon.2, JDK 8) with SonarLint performing static code analysis. It…
andand
  • 17,134
  • 11
  • 53
  • 79
1
vote
0 answers

How to find out exact root cause exception from a wrapper exception?

Lets take this example public MyClass{ public void myMethod() throws ExceptionC{ try{ //some method calll throwing ExceptionA, ExceptionB }catch(ExceptionA | ExceptionB e){ throw new ExceptionC(e); …
Supun Wijerathne
  • 11,964
  • 10
  • 61
  • 87
1
vote
4 answers

How do I use try/check for DateTime to prevent Server Error?

I'm attempting to write a try/catch, and failing obviously. I'm not sure I fully understand try/catch, but I do know that I'm having troubles wrapping my head around what I would need to happen in order to verify that a correct date is being entered…
Sierra
  • 327
  • 4
  • 11
1
vote
3 answers

Exception hierarchy/try-multi-catch

try { throw new FileNotFoundException(); } catch (IOException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } Can someone tell me why the second catch block is not considered as…
user1825770
  • 359
  • 1
  • 6
  • 17
1
vote
1 answer

Compilation error:Exception is never thrown in body of corresponding try statement

In my quest to learn Java better, I have been trying to understand exception handling. I cannot understand why the following code fails to compile. The compiler message is: TestExceptionHandling.java:12: error: exception StupidException is…
teraspora
  • 415
  • 4
  • 17
1
vote
1 answer

Understanding catching checked exceptions

I'm reading J. Bloch's Effective Java and now I'm at the section about checked/unchecked exceptions. He said that (emphasize mine): By confronting the API user with a checked exception, the API designer pre- sents a mandate to recover from the…
St.Antario
  • 26,175
  • 41
  • 130
  • 318
1
vote
1 answer

checked exceptions are not propagated in the chain

why checked exceptions are not propagated in the chain ? public static void m() { FileReader file = new FileReader("C:\\test\\a.txt"); BufferedReader fileInput = new BufferedReader(file); } public static void n() { m(); } public static…
supraja
  • 93
  • 2
  • 11
1
vote
1 answer

Hashing String (SHA-256) in an ActionListener class

I want to use the following code in an ActionListener class. MessageDigest digest = MessageDigest.getInstance("SHA-256"); byte[] hash = digest.digest(key.getBytes("UTF-8")); BigInteger HashValue = new…
fihdi
  • 145
  • 1
  • 1
  • 12
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