Skip to content

Commit 2e0d76b

Browse files
Distinguish between errors & exceptions (#2954)
This is to try to describe the difference between errors and exceptions more accurately.
1 parent 85e54aa commit 2e0d76b

File tree

4 files changed

+49
-43
lines changed

4 files changed

+49
-43
lines changed

concepts/exceptions/.meta/config.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
22
"blurb": "Exceptions are thrown when an error that needs special handling occurs.",
33
"authors": ["sanderploegsma"],
4-
"contributors": []
4+
"contributors": ["BahaaMohamed98"]
55
}

concepts/exceptions/about.md

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@ An exception is an event that occurs during the execution of a program that disr
88
Exceptions are raised explicitly in Java, and the act of raising an exception is called _throwing an exception_.
99
The act of handling an exception is called _catching an exception_.
1010

11-
Java distinguishes three types of exceptions:
11+
In Java, all exceptions are subclasses of the `Exception` class, which itself is a subclass of `Throwable`.
12+
13+
Java distinguishes two types of exceptions:
1214

1315
1. Checked exceptions
1416
2. Unchecked exceptions
15-
3. Errors
1617

1718
### Checked exceptions
1819

@@ -21,7 +22,7 @@ An example of a checked exception is the `FileNotFoundException` which occurs wh
2122

2223
This type of exception is checked at compile-time: methods that throw checked exceptions should specify this in their method signature, and code calling a method that might throw a checked exception is required to handle it or the code will not compile.
2324

24-
All exceptions in Java that do not inherit from `RuntimeException` or `Error` are considered checked exceptions.
25+
All checked exceptions are subclasses of `Exception` that do not extend `RuntimeException`.
2526

2627
### Unchecked exceptions
2728

@@ -30,17 +31,7 @@ An example of an unchecked exception is the `NullPointerException` which occurs
3031

3132
This type of exception is not checked at compile-time: methods that throw unchecked exceptions are not required to specify this in their method signature, and code calling a method that might throw an unchecked exception is not required to handle it.
3233

33-
All exceptions in Java that inherit from `RuntimeException` are considered unchecked exceptions.
34-
35-
### Errors
36-
37-
_Errors_ are exceptional conditions that are external to an application.
38-
An example of an error is the `OutOfMemoryError` which occurs when an application is trying to use more memory than is available on the system.
39-
40-
Like unchecked exceptions, errors are not checked at compile-time.
41-
They are not usually thrown from application code.
42-
43-
All exceptions in Java that inherit from `Error` are considered errors.
34+
All unchecked exceptions inherit from `RuntimeException`, which itself is an extension of `Exception`.
4435

4536
## Throwing exceptions
4637

@@ -135,6 +126,17 @@ Withdrawal failed: Cannot withdraw a negative amount
135126
Current balance: 5.0
136127
```
137128

129+
## Errors
130+
131+
Java also has a separate category called _Errors_ which are serious problems that are external to an application.
132+
An example of an error is the `OutOfMemoryError` which occurs when an application is trying to use more memory than is available on the system.
133+
134+
Like unchecked exceptions, errors are not checked at compile-time.
135+
The difference is that they represent system level problems and are generally thrown by the Java Virtual machine or environment instead of the application.
136+
Applications should generally not attempt to catch or handle them.
137+
138+
All errors in Java inherit from the `Error` class.
139+
138140
## When not to use exceptions
139141

140142
As stated previously, exceptions are events that disrupt the normal flow of instructions, and are used to handle _exceptional events_.

concepts/exceptions/introduction.md

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@ An exception is an event that occurs during the execution of a program that disr
88
Exceptions are raised explicitly in Java, and the act of raising an exception is called _throwing an exception_.
99
The act of handling an exception is called _catching an exception_.
1010

11-
Java distinguishes three types of exceptions:
11+
In Java, all exceptions are subclasses of the `Exception` class, which itself is a subclass of `Throwable`.
12+
13+
Java distinguishes two types of exceptions:
1214

1315
1. Checked exceptions
1416
2. Unchecked exceptions
15-
3. Errors
1617

1718
### Checked exceptions
1819

@@ -21,7 +22,7 @@ An example of a checked exception is the `FileNotFoundException` which occurs wh
2122

2223
This type of exception is checked at compile-time: methods that throw checked exceptions should specify this in their method signature, and code calling a method that might throw a checked exception is required to handle it or the code will not compile.
2324

24-
All exceptions in Java that do not inherit from `RuntimeException` or `Error` are considered checked exceptions.
25+
All checked exceptions are subclasses of `Exception` that do not extend `RuntimeException`.
2526

2627
### Unchecked exceptions
2728

@@ -30,17 +31,7 @@ An example of an unchecked exception is the `NullPointerException` which occurs
3031

3132
This type of exception is not checked at compile-time: methods that throw unchecked exceptions are not required to specify this in their method signature, and code calling a method that might throw an unchecked exception is not required to handle it.
3233

33-
All exceptions in Java that inherit from `RuntimeException` are considered unchecked exceptions.
34-
35-
### Errors
36-
37-
_Errors_ are exceptional conditions that are external to an application.
38-
An example of an error is the `OutOfMemoryError` which occurs when an application is trying to use more memory than is available on the system.
39-
40-
Like unchecked exceptions, errors are not checked at compile-time.
41-
They are not usually thrown from application code.
42-
43-
All exceptions in Java that inherit from `Error` are considered errors.
34+
All unchecked exceptions inherit from `RuntimeException`, which itself is an extension of `Exception`.
4435

4536
## Throwing exceptions
4637

@@ -134,3 +125,14 @@ Withdrawing -10.0
134125
Withdrawal failed: Cannot withdraw a negative amount
135126
Current balance: 5.0
136127
```
128+
129+
## Errors
130+
131+
Java also has a separate category called _Errors_ which are serious problems that are external to an application.
132+
An example of an error is the `OutOfMemoryError` which occurs when an application is trying to use more memory than is available on the system.
133+
134+
Like unchecked exceptions, errors are not checked at compile-time.
135+
The difference is that they represent system level problems and are generally thrown by the Java Virtual machine or environment instead of the application.
136+
Applications should generally not attempt to catch or handle them.
137+
138+
All errors in Java inherit from the `Error` class.

exercises/concept/calculator-conundrum/.docs/introduction.md

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@ An exception is an event that occurs during the execution of a program that disr
1010
Exceptions are raised explicitly in Java, and the act of raising an exception is called _throwing an exception_.
1111
The act of handling an exception is called _catching an exception_.
1212

13-
Java distinguishes three types of exceptions:
13+
In Java, all exceptions are subclasses of the `Exception` class, which itself is a subclass of `Throwable`.
14+
15+
Java distinguishes two types of exceptions:
1416

1517
1. Checked exceptions
1618
2. Unchecked exceptions
17-
3. Errors
1819

1920
#### Checked exceptions
2021

@@ -23,7 +24,7 @@ An example of a checked exception is the `FileNotFoundException` which occurs wh
2324

2425
This type of exception is checked at compile-time: methods that throw checked exceptions should specify this in their method signature, and code calling a method that might throw a checked exception is required to handle it or the code will not compile.
2526

26-
All exceptions in Java that do not inherit from `RuntimeException` or `Error` are considered checked exceptions.
27+
All checked exceptions are subclasses of `Exception` that do not extend `RuntimeException`.
2728

2829
#### Unchecked exceptions
2930

@@ -32,17 +33,7 @@ An example of an unchecked exception is the `NullPointerException` which occurs
3233

3334
This type of exception is not checked at compile-time: methods that throw unchecked exceptions are not required to specify this in their method signature, and code calling a method that might throw an unchecked exception is not required to handle it.
3435

35-
All exceptions in Java that inherit from `RuntimeException` are considered unchecked exceptions.
36-
37-
#### Errors
38-
39-
_Errors_ are exceptional conditions that are external to an application.
40-
An example of an error is the `OutOfMemoryError` which occurs when an application is trying to use more memory than is available on the system.
41-
42-
Like unchecked exceptions, errors are not checked at compile-time.
43-
They are not usually thrown from application code.
44-
45-
All exceptions in Java that inherit from `Error` are considered errors.
36+
All unchecked exceptions inherit from `RuntimeException`, which itself is an extension of `Exception`.
4637

4738
### Throwing exceptions
4839

@@ -136,3 +127,14 @@ Withdrawing -10.0
136127
Withdrawal failed: Cannot withdraw a negative amount
137128
Current balance: 5.0
138129
```
130+
131+
### Errors
132+
133+
Java also has a separate category called _Errors_ which are serious problems that are external to an application.
134+
An example of an error is the `OutOfMemoryError` which occurs when an application is trying to use more memory than is available on the system.
135+
136+
Like unchecked exceptions, errors are not checked at compile-time.
137+
The difference is that they represent system level problems and are generally thrown by the Java Virtual machine or environment instead of the application.
138+
Applications should generally not attempt to catch or handle them.
139+
140+
All errors in Java inherit from the `Error` class.

0 commit comments

Comments
 (0)