We Offer 100% Job Guarantee Courses (Any Degree / Diploma Candidates / Year GAP / Non-IT / Any Passed Outs). Placement Records
Hire Talent (HR):+91-9707 240 250

General

Exception Handling in Java

Exception Handling in Java

Exception Handling in Java

An exception is an unpredicateble  or unwanted  event, which is happend at the time of execution of a application or program i.e  run time  it will  collaps the regular flow of the application execution of instructions of programmer.

We can consider the exceptions are for run time errors like stack winding,stack over flow ,stack underflow ,unable to connect the database,file opening error,divisible by zero,array out of bound error.

Types of Exception in Java

Java has been divided two types of exceptions they are

  • Checked Exceptions
  • UnChecked Exceptions

Checked Exceptions:

Checked exceptions we can categorised as Errors, run time errors. Most common unchecked Exceptions are IoException,SQLException,Data Access Exception,Class not found exception,Installation exceptional.

UnChecked Exceptions:

Unchecked Expections are completely consider as Run time exception or errors,most common are Null pointer exception,Array index bound,llegal State Exception,Number Format Exception.

What is user or custom defined exception in Java?

Java we have already outlined, exception categories like Arithmetic Exception, Null Pointer Exception etc. This exceptions defined area  things already set for trigger on conditions  In built like you once divide any objecy by zero division divisionbyzero exception from arithmetic, In Last exception we have  to learnt however to throw these exceptions expressly supported your conditions exploitation keyword  throw.

we are able to produce our user exception category and hat exception  throw texploitation keyword  throw. These exceptions area unit called custom exceptions or user-defined . To create our user exceptions we can use  try-catch block and throw.

Example:

class InvalidProductException extends Exception
{
    public InvalidProductException(String s)
    {
        // Call constructor of parent Exception
        super(s);
    }
}
public class Example1
{
   void productCheck(int weight) throws InvalidProductException{
                if(weight<100){
                                throw new InvalidProductException("Product Invalid");
                }
   }
    public static void main(String args[])
    {
                Example1 obj = new Example1();
        try
        {
            obj.productCheck(60);
        }
        catch (InvalidProductException ex)
        {
            System.out.println("Caught the exception");
            System.out.println(ex.getMessage());
        }
    }
}

Output:

Caught the exception
Product Invalid

Click Here-> Get Java Training with Real-time Projects

Besant Technologies WhatsApp