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

Amritha Java 8ab

Uploaded by

bhavya v
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)
0 views3 pages

Amritha Java 8ab

Uploaded by

bhavya v
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

import java.util.

Scanner;

public class valid {

public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.print("Enter a number: ");

try {

String input = scanner.next();

int number = Integer.parseInt(input);

System.out.println("The given input is valid!");

} catch (NumberFormatException e) {

System.out.println("Invalid input! Please enter only numbers.");

System.out.println("Error: " + e); } finally {

scanner.close();

}
import java.util.Scanner;

class negative extends Exception {

public negative(String msg) {

super(msg);

public class numcheck {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.print("Enter a positive number: ");

try {

int num = sc.nextInt();

if (num < 0) throw new negative("Negative numbers not allowed!");

System.out.println("Valid number: " + num);

} catch (negative e) {

System.out.println("Invalid input! " + e.getMessage());

System.out.println("Error: " + e);

} catch (Exception e) {

System.out.println("Invalid input! Enter numbers only.");

System.out.println("Error: " + e);

} finally {

sc.close();

You might also like