Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions exceptions-end/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.baeldung;

import java.time.LocalDate;
import java.time.format.DateTimeParseException;
import java.util.InputMismatchException;
import java.util.Scanner;

public class Main {

public static void main(String[] args) {
String invalidDate = "2050/12/31";
try {
LocalDate date = LocalDate.parse(invalidDate);
System.out.println("Date parsed: " + date);
} catch (DateTimeParseException e) {
System.out.println("Error: The date format isn’t correct.");
}

Scanner scanner = new Scanner(System.in);
try {
System.out.print("Enter a number: ");
int number = scanner.nextInt();
System.out.println("You entered: " + number);
} catch (InputMismatchException e) {
System.out.println("Error: That's not a valid number.");
} finally {
System.out.println("Closing scanner...");
scanner.close();
}

try {
validatePriority(-5);
} catch (IllegalArgumentException e) {
System.out.println("Error: " + e.getMessage());
}

String text = null;
// int length = text.length(); // uncomment to trigger NPE

int[] numbers = {1, 2, 3};
// int number = numbers[5]; // uncomment to trigger ArrayIndexOutOfBoundsException
}

public static void validatePriority(int priority) {
if (priority < 0) {
throw new IllegalArgumentException("Priority cannot be negative");
}
System.out.println("Priority is valid: " + priority);
}
}