When a programmer throws any custom exception he must declare that exception in method signature
An exception is an issue (run time error) occurred during the execution of a program. When an exception occurred the program gets terminated abruptly and, the code past the line that generated the exception never gets executed. Show
User defined exceptionsYou can create your own exceptions in Java and they are known as user defined exceptions or custom exceptions. To create a user defined exception extend one of the above mentioned classes. To display the message override the toString() method or, call the superclass parameterized constructor by passing the message in String format. Then, in other classes wherever you need this exception to be raised, create an object of the created custom exception class and, throw the exception using the throw keyword. MyException ex = new MyException (); If(condition……….){ throw ex; }Custom Checked and Custom Unchecked
Example: Custom Checked exceptionFollowing Java program Demonstrates how to create Custom checked exception. import java.util.Scanner; class NotProperNameException extends Exception { NotProperNameException(String msg){ super(msg); } } public class CustomCheckedException{ private String name; private int age; public static boolean containsAlphabet(String name) { for (int i = 0; i < name.length(); i++) { char ch = name.charAt(i); if (!(ch >= 'a' && ch <= 'z')) { return false; } } return true; } public CustomCheckedException(String name, int age){ if(!containsAlphabet(name)&&name!=null) { String msg = "Improper name (Should contain only characters between a to z (all small))"; NotProperNameException exName = new NotProperNameException(msg); throw exName; } this.name = name; this.age = age; } public void display(){ System.out.println("Name of the Student: "+this.name ); System.out.println("Age of the Student: "+this.age ); } public static void main(String args[]) { Scanner sc= new Scanner(System.in); System.out.println("Enter the name of the person: "); String name = sc.next(); System.out.println("Enter the age of the person: "); int age = sc.nextInt(); CustomCheckedException obj = new CustomCheckedException(name, age); obj.display(); } }Compile time exceptionOn compiling, the above program generates the following exception. Example: Custom unChecked exceptionIf you simply change the class that your custom exception inherits to RuntimeException it will be thrown at run time class NotProperNameException extends RuntimeException { NotProperNameException(String msg){ super(msg); } }If you run the previous program By replacing the NotProperNameException class with above piece of code and run it, it generates the following runtime exception. Runtime exceptionEnter the name of the person: Krishna1234 Enter the age of the person: 20 Exception in thread "main" july_set3.NotProperNameException: Improper name (Should contain only characters between a to z (all small)) at july_set3.CustomCheckedException.
Updated on 03-Jul-2020 08:02:42
When a programmer throws any custom exception he must declare?Using throws keyword we can declare the method which might be exception producing. In order to use a custom exception, you must show classes that call your code that they need to plan for this new type of exception. You do this by declaring that one or more of your methods throws the exception.
When a programmer throws any exception he must declare that exception in method signature?Checked exceptions are always declared as thrown in the method signature. The signature lets the method's caller know that an exception may occur as a consequence of the call. If the exception does get thrown, the caller must be prepared to do something about it.
What happens when a method throws an exception?When a method declares that it throws an exception, it is not required to handle the exception. The caller of a method that throws exceptions is required to handle the exceptions (or throw them to its caller and so on) so that the flow of the program can be maintained.
Why throws declared with method signature in Java?If anyone were to see this method from the outside, they might try to use it without knowing that it is not supported. That's exactly the main reason, why throws is designed in a way to be checked at compile-time for all Checked Exceptions - clients of the method will be aware what this method may possibly throw.
|