danger mouse
danger mouse

Reputation: 1497

Java checked and unchecked exceptions

  1. If I create an exception class that extends Exception, will my class be checked or unchecked? I note that the subclass of Exception called RuntimeException is an unchecked exception whereas all other subclasses of 'Exception' are checked exceptions.

  2. If I create an exception class that extends RuntimeException, can I specify that this class is checked?

Upvotes: 4

Views: 518

Answers (4)

techPackets
techPackets

Reputation: 4516

If your class extends Exception it can throw checked exceptions.

If your class extends Error or RuntimeException it can throw unchecked exceptions.

enter image description here

Upvotes: 1

Evgeniy Dorofeev
Evgeniy Dorofeev

Reputation: 136112

Exceptions are checked unless they inherit from RuntimeException or Error

Upvotes: 1

Sunil Rajashekar
Sunil Rajashekar

Reputation: 350

If you create class that extends Exception it would be checked. You can't specify RuntimeException as checked since it is unchecked exception

Upvotes: 0

Grzegorz Piwowarek
Grzegorz Piwowarek

Reputation: 13853

1) Checked

2) No

If you extend Exception -> checked

If you extend RuntimeException -> unchecked

From documentation:

The class {@code Exception} and any subclasses that are not also * subclasses of {@link RuntimeException} are checked * exceptions

enter image description here

Upvotes: 3

Related Questions