Posts

Showing posts with the label Exception Handling

Program to throw a user-defined exception "String Mismatch Exception", if two strings are not equal (ignore the case)

This is a simple program in which we have two checked two strings for equality. If they are equal then print "String matched !!!" otherwise throw user-defined exception. The  euqlasIgnoreCase( )  method of  java.lang.String  class is used to compare two strings by ignoring case considerations. PROGRAM import java.util.Scanner; class StringMismatchException extends Exception { public StringMismatchException(String str) { System.out.println(str); } } public class StringExcDemo { public static void main(String[] args) { Scanner scan = new Scanner(System.in); System.out.print("Enter the string :: "); String input = scan.nextLine(); try { if(input.equalsIgnoreCase("Hello")) System.out.println("String matched !!!"); else throw new StringMismatchException("String not matched ???"); } catch (StringMismatchException s) { System.out.println(s); } } } OUTPUT 1 C:\>javac StringExcDemo....

Program to throw user defined exception if the given number is not positive

The given program contains an exception class  MyException  which throws negative and positive number exception. We have to read a number from user at run time and throw an exception if the the entered number is negative or positive. Here we have used the  BufferedReader  class to read data from keyboard. The  readLine( )  method is used to read a string from keyboard and  parseInt( )  method converts String object into integer number.  parseInt( )  is the method of  Integer  wrapper class. PROGRAM import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; class MyException extends Exception { public MyException(String str) { System.out.println(str); } } public class SignException { public static void main(String[] args)throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.print("Input number :: "); try { int num...

Program to input name and age of a person and throw an user-defined exception, if the entered age is negative

The given program contains an exception class  AgeNegativeException  which throws an negative age exception. We have to read name and age from user at run time and throw an exception if the entered age is negative. Here we have used the Scanner class and its methods like  nextLine( ), nextInt( )  to read string and number respectively. The  getMessage( )  method of  Throwable  class is used to return a description of the exception. PROGRAM import java.util.Scanner; class AgeNegativeException extends Exception { public AgeNegativeException(String msg) { super(msg); } } public class NameAgeExcDemo { public static void main(String[] args) { Scanner s = new Scanner(System.in); System.out.print("Enter ur name :: "); String name = s.nextLine(); System.out.print("Enter ur age :: "); int age = s.nextInt(); try { if(age < 0) throw new AgeNegativeException("Age must be greater than 0"); else System.out.pr...

Program to accept a password from the user and throw 'Authentication Failure' exception if the password is incorrect

In this program, we have to throw an exception if user enters wrong password. The  IOException  signals that an I/O exception of some sort has occurred. This class is the general class of exceptions produced by failed or interrupted I/O operations. The  printStackTrace( )  method of  Throwable  class which prints throwable and its backtrace to the standard error stream. PROGRAM import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; class AuthenticationException extends Exception { public AuthenticationException(String message) { super(message); } } public class AuthenticationExcDemo { public static void main(String[] args) { InputStreamReader isr = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(isr); String pwd; try { System.out.print("Enter password :: "); pwd = br.readLine(); if(!pwd.equals("123")) throw new AuthenticationException("...

Program to raise a user defined exception if username is less than 6 characters and password does not match

Java exception handling is managed via five keywords:  try, catch, throw, throws  and  finally . try  - Program statements that you want to monitor for exceptions are contained within a try block. If an exception occurs within the try block, it is thrown. catch  - Your code can catch this exception (using catch) and handle it in some rational manner. throw  - System-generated exceptions are automatically thrown by the Java run-time system. To manually throw an exception, use the keyword throw. throws  - Any exception that is thrown out of a method must be specified as such by a throws clause. finally  - Any code that absolutely must be executed before a method returns is put in a finally block. The syntax of Exception Handling block is as follows: try { // block of code to monitor for errors } catch (ExceptionType1 exOb) { // exception handler for ExceptionType1 } catch (ExceptionType2 exOb) { // exception handler for ExceptionTyp...

Program to input age from user and throw user-defined exception if entered age is negative

An  Exception  is an abnormal condition that arises in a code sequence at run time. Exceptions are run time errors. Exceptions generated by the Java are related to the fundamental errors that violate the Java language constraints. All exception classes are subtypes of the  java.lang.Exception  class. The exception class is a subclass of the  Throwable  class. Java defines several exception classes inside the standard package  java.lang . Some examples of exceptions are  ArithmeticException, ArrayIndexOutOfBoundsException, IndexOutOfBoundsException, NullPointerException, NumberFormatException, ClassNotFoundException,etc. When Java Interpreter caught an error such as divide by zero, the interpreter creates an exception object and throws it to inform that an error has occurred. Java has its own exception handling mechanism. It performs the following task: Find the problem –  Hit  the exception Inform that an error has occurred ...