Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit ba98451

Browse files
Add exception handling to Java tutorial
1 parent b68d8c4 commit ba98451

File tree

1 file changed

+96
-33
lines changed

1 file changed

+96
-33
lines changed

‎Java/01 Java - Introduction (WIP).md‎

Lines changed: 96 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -993,6 +993,8 @@ int found_index = Arrays.binarySearch(nums, 5); // Binary search!
993993

994994
Arrays.equals(copied_nums, nums); // Array equality!
995995
Arrays.sort(nums); // In-place sort!
996+
997+
Arrays.toString(nums); // Get string representation
996998
```
997999

9981000
And lots more! Press `Ctrl-Space` in IntelliJ after typing `Arrays.` to see the list! Then you can use `Ctrl-Q` to see the relevant javadocs.
@@ -1127,7 +1129,100 @@ while (i < 5);
11271129

11281130

11291131

1130-
### Methods (Functions)
1132+
### Sleeping
1133+
1134+
Delay your program flow!
1135+
1136+
Simple use `Thread.sleep()`! But ensure that you append your method signature with `throws InterruptedException`!
1137+
1138+
```java
1139+
public class SleepMessages {
1140+
public static void main(String args[]) throws InterruptedException {
1141+
String theBreathOfSong[] = {
1142+
"Within a land of Wind and Sky",
1143+
"A Dragon spreads his wings",
1144+
"He flies on through strife-stricken Fields",
1145+
"Takes Pause",
1146+
"And soars to Dawn"
1147+
};
1148+
1149+
for (int i = 0; i < theBreathOfSong.length; i++) {
1150+
Thread.sleep(1000); // Pause for 1 second
1151+
System.out.println(importantInfo[i]);
1152+
}
1153+
}
1154+
}
1155+
```
1156+
1157+
1158+
1159+
### Exception Handling
1160+
1161+
In the previous section we saw that we said that the `main()` function could throw an `InterruptedException`. This section is all about handling exceptions such as that!
1162+
1163+
**General Exception Handling with `try catch`**
1164+
1165+
```java
1166+
public class Main {
1167+
public static void main(String[ ] args) {
1168+
try {
1169+
int[] myNumbers = {1, 2, 3};
1170+
System.out.println(myNumbers[10]);
1171+
} catch (Exception e) {
1172+
System.out.println("Something went wrong.");
1173+
}
1174+
}
1175+
}
1176+
```
1177+
1178+
You can also use `finally` to run things at the end even if an exception was caught!
1179+
1180+
```java
1181+
public class Main {
1182+
public static void main(String[] args) {
1183+
try {
1184+
int[] myNumbers = {1, 2, 3};
1185+
System.out.println(myNumbers[10]);
1186+
} catch (Exception e) {
1187+
System.out.println("Something went wrong.");
1188+
} finally {
1189+
System.out.println("The 'try catch' is finished.");
1190+
}
1191+
}
1192+
}
1193+
```
1194+
1195+
**Throw and Throws**
1196+
1197+
Use `throw` to throw an exception.
1198+
1199+
```java
1200+
throw new ArithmeticException("/ by zero");
1201+
```
1202+
1203+
Use `throws` to indicate that the method it is attached to **might** throw some exception. The caller to the method has the responsibility of handling the exception in a `try catch` block.
1204+
1205+
```java
1206+
type method_name(parameters) throws exception_list {}
1207+
1208+
// exception_list is a comma separated list of all the exceptions which a method might throw.
1209+
```
1210+
1211+
1212+
1213+
### Assertions
1214+
1215+
Mandate that some statement evaluates to true, otherwise, throw an assertion error.
1216+
1217+
This is usually used for test cases!
1218+
1219+
```java
1220+
assert assertion_expression : "printed string if false"
1221+
```
1222+
1223+
1224+
1225+
### Methods (Functions) [This should be moved to another function specific tutorial section]
11311226

11321227
Methods are just functions within a class. But because everything in Java must be within a class, every Java function is a method.
11331228

@@ -1259,38 +1354,6 @@ foo("a", null, 2);
12591354

12601355

12611356

1262-
### Java Handing
1263-
1264-
1265-
1266-
2.1 [Comments](#2.1)
1267-
2.2 [Importing Libraries](#2.2)
1268-
2.3 [Hello World!](#2.3)
1269-
2.4 [Running Programs](#2.4)
1270-
2.5 [Variables and Data Types](#2.5)
1271-
2.6 [Ouputting Variables and Variable Info](#2.6)
1272-
2.7 [Casting (Type Conversion)](#2.7)
1273-
2.8 [Arithmetic Operations](#2.8)
1274-
2.9 [Conditionals](#2.9)
1275-
2.10 [Ternary Operators](#2.10)
1276-
2.11 [Switch Cases](#2.11)
1277-
2.12 [Arrays](#2.12)
1278-
2.13 [Multi-Dimensional Arrays](#2.13)
1279-
2.14 [Strings](#2.14)
1280-
2.15 [User Input](#2.15)
1281-
2.16 [For Loops](#2.16)
1282-
2.17 [While Loops](#2.17)
1283-
2.18 [Do While Loops](#2.18)
1284-
2.19 [Vectors](#2.19)
1285-
2.20 [Printf](#2.20)
1286-
2.21 [Argc and Argv](#2.21)
1287-
2.22 [Exception Handling](#2.22)
1288-
2.22 [Scopes](#2.23)
1289-
1290-
1291-
1292-
1293-
12941357

12951358
```
12961359
. .

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /