package Latihan; import java.util.Scanner; import java.util.NoSuchElementException; /** * * @author sayfudin */ public class soalNoSuch { public soalNoSuch() { Scanner scanner = new Scanner(System.in); int row = scanner.nextInt(); // exception at this line scanner.close(); // ini yang buat error karena hanya bisa input 1 kali // meskipun konstruktor di jalankan lebih 1 kali. } public static void main(String[] args) { soalNoSuch ex1 = new soalNoSuch(); // this line successfully operate try{ soalNoSuch ex2 = new soalNoSuch(); // exception : no such element exception at above line }catch(NoSuchElementException e){ System.out.println("terjadi kesalahan element " + e); } } }
semoga manfaat ammin :)