Java Exception Handling 03. Sample Program without exception handling

Опубликовано: 28 Июнь 2026
на канале: Snipcademy
181
0

This video will introduce you to a program that contains no exception handling. Notice that it won't compile, since it does not handle the checked IO exception.

The next video will show you how to implement exceptions.

The following code may have been altered to comply with youtube's video description policy. To download the actual files, go here:
   • Java Exception Handling 00. Setting up and...  

import java.util.Scanner;
import java.io.File;

/**
Test class that reads 10 integers from a file.

@author CodeSnippetsAcademy
@version 1.0.0
*/
public class Read10Ints {

public static void main(String[] args) {

String fileName = args[0];

/** Checked exception for IOException - won't compile
unless we obey the Catch or Specify Requirement */
File inputFile = new File(fileName);
Scanner in = new Scanner(inputFile);

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

/** Unchecked exceptions for ArrayIndexOutOfBoundsException
and InputMismatchException - still compiles */
for (int i = 0; i less than SIZE; i++) {
arrayOfInts[i] = in.nextInt();
System.out.println(arrayOfInts[i]);
}

in.close();

}

}