From caf2b9120cb42fb1828f867f5d29070687536666 Mon Sep 17 00:00:00 2001 From: yabetancourt Date: Thu, 8 Jan 2026 21:20:24 +0100 Subject: [PATCH] COURSE-276 Add Main class with exception handling examples --- exceptions-end/Main.java | 50 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 exceptions-end/Main.java diff --git a/exceptions-end/Main.java b/exceptions-end/Main.java new file mode 100644 index 0000000..6b068d0 --- /dev/null +++ b/exceptions-end/Main.java @@ -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); + } +}