Java Exception Handling 04. Sample program with exception handling

preview_player
Показать описание
This video shows a simple implementation of exception handling.

The next video will show you how to use a more elegant syntax to handle exceptions - the try-with-resources feature.

The following code may have been altered to comply with youtube's video description policy. To download the actual files, go here:

/**
* Sample class that demonstrates exception handling
*
* @author CodeSnippetsAcademy
* @version 1.0.0
*/
public class Read10IntsWithHandling {
public static void main(String[] args) {

Scanner in = null;

// Let's try something...
try {

/** Unchecked exception - user may forget
to type file name */
String fileName = args[0];

/** Wrap anything that is related to IO
in a try-catch statement */
File inputFile = new File(fileName);
in = new Scanner(inputFile);

final int SIZE = 10;
int[] arrayOfInts = new int[SIZE];

// Read in and output 10 integers
for (int i = 0; i *less than* SIZE; i++) {
}

} catch (ArrayIndexOutOfBoundsException ex) {


} catch (FileNotFoundException ex) {


} catch (InputMismatchException ex) {


} finally {

if (in != null) {
} else {
}

}

}
}
Рекомендации по теме
visit shbcf.ru