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
23
votes
6 answers

How to identify checked and unchecked exceptions in java?

While reading about exception, I will always come across checked exceptions and unchecked exceptions, So wanted to know how to distinguish that which is what? Edit: I want to know if i create any exception class then how can i create as a checked or…
GuruKulki
  • 25,776
  • 50
  • 140
  • 201
22
votes
3 answers

Languages that use checked exceptions

I'm just curious, is there any other languages besides Java that uses checked exceptions ? I did try to find information about this but couldn't find any answers.
Christiaan
  • 639
  • 1
  • 8
  • 18
22
votes
3 answers

How to throw an Exception when your method signature doesn't allow to throw Exception?

I have a method like this: public void getSomething(){ ... } I want to throw an Exception inside getSomething(). The compiler will not allow me to do that because my method doesn't allow Exception to be thrown in there. But I need to throw a…
Sean Nguyen
  • 12,528
  • 22
  • 74
  • 113
21
votes
3 answers

Getting Mockito Exception : checked exception is invalid for this method

I have a method which I am trying to test public List getUsers(String state) { LOG.debug("Executing getUsers"); LOG.info("Fetching users from " + state); List users = null; try { users =…
sk17261205
  • 421
  • 1
  • 5
  • 12
19
votes
5 answers

Checked vs. Unchecked Exceptions in Service Layer

I work on a project with a legacy service layer that returns null in many places if a requested record does not exist, or cannot be accessed due to the caller not being authorized. I am talking about specific records requested by ID. For instance,…
S73417H
  • 2,661
  • 3
  • 23
  • 37
19
votes
4 answers

Why does the compiler allow throws when the method will never throw the Exception

I am wondering why the java compiler allows throws in the method declaration when the method will never throw the Exception. Because "throws" is a way of handling the exception (telling the caller to handle it). Since there are two ways of handling…
PsyAp
  • 193
  • 1
  • 6
19
votes
3 answers

Why is throwing a checked exception type allowed in this case?

I noticed by accident that this throw statement (extracted from some more complex code) compiles: void foo() { try { } catch (Throwable t) { throw t; } } For a brief but happy moment I thought that checked exceptions had…
Boann
  • 48,794
  • 16
  • 117
  • 146
16
votes
7 answers

Checked vs Unchecked exception

What does the following quote mean? With an unchecked exception, however, the compiler doesn't force client programmers either to catch the exception or declare it in a throws clause. In fact, client programmers may not even know that the exception…
Laawanya
  • 241
  • 1
  • 3
  • 8
16
votes
1 answer

Will a subclass catch block catch a checked parent exception?

I have a method with a checked exception for a parent class, which can throw exceptions of the type parent and subclass public void method() throws ParentException { if( false ) throw new ParentException(); else if( true ) throw new…
Sajhu
  • 255
  • 1
  • 3
  • 8
15
votes
1 answer

What's the idea behind Kotlin removing checked exceptions?

And how do we handle this when we are calling a method in Java which throws exceptions ? my code is in kotlin and I am using a 3rd party library which is written in java. I call a method of this library which throws few custom exceptions in some…
user12297160
14
votes
5 answers

Handling Java exceptions caught in constructors, with final members

I have some ugly code and want to refactor it: public class UdpTransport extends AbstractLayer { private final DatagramSocket socket; private final InetAddress address; private final int port; /* boolean dead is provided by…
fadedbee
  • 42,671
  • 44
  • 178
  • 308
13
votes
1 answer

Good pattern? ... method() throws X

Some background, then some questions. I have only recently discovered that an interface (or class) may be generic in the type of (checked) exception that its methods may throw. For example: interface GenericRunnable { void…
user2357
  • 452
  • 5
  • 11
12
votes
4 answers

How to handle IOException in Iterable.forEach?

I am playing around with ways to write a set of objects to a file. Why does the below implementation using Iterable.forEach() not compile? In Eclipse, I get the message that an IOException is not being handled. This is particularly confusing since I…
Will Beason
  • 3,417
  • 2
  • 28
  • 46
10
votes
3 answers

How to catch all checked exceptions (in a single block) in Java 6?

IMPORTANT: This question only relates to Java 6 (and below). The hierarchy here shows Java Exceptions are divided into two types: RuntimeException and [not a RuntimeException]: Would it not have been better to divide into something like…
Steve Chambers
  • 37,270
  • 24
  • 156
  • 208
10
votes
4 answers

Hard time understanting checked & unchecked exceptions

I've already read everything I could about this and I still don't understand how to use checked and unchecked exceptions. I think I can't still grasp the concept. I've read around StackOverflow that it's better to use unchecked rather than checked…
1
2
3
9 10