+1 vote
in JAVA by
Explain the exception handling mechanism of Java?

1 Answer

0 votes
by

Explain the exception handling mechanism of Java?

Exception class inherits from the Throwable class in java. Java has a try-catch mechanism for handling exceptions without them being generated as errors.

public class Exception_Handling { 

    String gender; 

    Exception_Handling(String s){ 

        gender=s; 

    } 

     void Check_Gender(String s) throws GenderException{ 

        if (gender!="Male" || gender!="Female") 

            throw new GenderException("Gender Invalid"); 

        else 

        System.out.println("Gender valid"); 

        } 

    public static void main(String args[]){ 

        Exception_Handling n=new Exception_Handling("None"); 

        try{ 

            n.Check_Gender("Female"); 

        }catch (Exception e){ 

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

        } 

    } 

    } 

class GenderException extends Exception{ 

    GenderException(String s){ 

        super(s); 

    } 

🔗Reference: stackoverflow.com

🔗Source: Java Interview Questions and Answers

Related questions

+2 votes
asked May 30, 2020 in JAVA by Robindeniel
+1 vote
+1 vote
asked Feb 1, 2023 in JAVA by sharadyadav1986
...