0% found this document useful (0 votes)
11 views3 pages

Java Exception Handling Examples

The document provides Java programs demonstrating exception handling through two examples: a Marks Validation Program that checks if student marks are between 0 and 100, and a Username Validation Program that ensures usernames are at least 5 characters long. Each program includes code snippets and sample outputs for both valid and error cases. The use of try-catch blocks is highlighted to manage exceptions effectively.

Uploaded by

vikashtamila
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views3 pages

Java Exception Handling Examples

The document provides Java programs demonstrating exception handling through two examples: a Marks Validation Program that checks if student marks are between 0 and 100, and a Username Validation Program that ensures usernames are at least 5 characters long. Each program includes code snippets and sample outputs for both valid and error cases. The use of try-catch blocks is highlighted to manage exceptions effectively.

Uploaded by

vikashtamila
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Java Programs with Exception Handling Examples

1) Marks Validation Program


This Java program reads marks of students, validates each mark to ensure it is between 0 and 100,
and throws an exception if any invalid mark is found.

Code:

import java.util.*;

public class Main {


public static void main(String[] args) {
Scanner s = new Scanner(System.in);

System.out.print("Enter number of marks: ");


int n = s.nextInt();

int[] marks = new int[n];

System.out.println("Enter marks for " + n + " students:");

try {
for (int i = 0; i < n; i++) {
marks[i] = s.nextInt();
if (marks[i] < 0 || marks[i] > 100) {
throw new Exception("Invalid mark at index " + i + ": " + marks[i]);
}
}
System.out.println("All marks are valid.");
} catch (Exception e) {
System.out.println("Error: " + e.getMessage());
}
}
}

Sample Output 1:

Enter number of marks: 3


Enter marks for 3 students:
45
67
98
All marks are valid.

Sample Output 2 (Error Case):

Enter number of marks: 2


Enter marks for 2 students:
67
108
ERROR!
Error: Invalid mark at index 1: 108

2) Username Validation Program


This Java program checks whether the entered username has at least 5 characters and throws an
exception if the condition is not met.

Code:

import java.util.Scanner;

public class Main {


public static void main(String[] args) {
Scanner s = new Scanner(System.in);

System.out.print("Enter username: ");


String username = s.nextLine();

try {
if (username.length() < 5) {
throw new Exception("Username must be at least 5 characters long.");
}
System.out.println("Username is valid: " + username);
} catch (Exception e) {
System.out.println("Error: " + e.getMessage());
}
}
}

Sample Output 1:

Enter username: madhu


Username is valid: madhu

Sample Output 2 (Error Case):

Enter username: my
ERROR!
Error: Username must be at least 5 characters long.

You might also like