JAVA Exceptions and Error Handling

Q.No.1. what are the four access modifiers available in Java and what is their significance in terms of class, method and variable accessibility?

ANS:

Access modifiers, also known by their alternative name of access specifiers, are an essential component of object-oriented programming languages. It is possible to set the accessibility levels of classes, interfaces, methods, and member variables with the help of these keywords. You can control which other courses outside your own have access to certain areas of your code by using access modifiers.

Access modifiers in Java are keywords that determine the scope and visibility of classes, methods, variables, and constructors within an application. They are fundamental to object-oriented programming as they help enforce encapsulation, a core principle restricting direct access to some of an object's components

Java has four access modifiers that set the accessibility of methods, classes, and other members:

  • Private: Most restricted access, only accessible within the class

  • Protected: Accessible within the same package or by subclasses in different packages

  • Default: Also known as "package-private", accessible within the same package

  • Public: Accessible from anywhere, with the widest scope

Here's how the modifiers affect accessibility:

  • Private

    The private access modifier is the most restrictive level of access control. When a member (be it a field, method, or constructor) is declared private, it can only be accessed within the same class. No other class, including subclasses in the same package or different packages, can access the member. This modifier is typically used for sensitive data that should be hidden from external classes.

  • Protected

    The protected access modifier is less restrictive than private and default. Members declared as protected can be accessed within the same package or in subclasses in different packages. This is particularly useful in cases where you want to hide a member from the world but still make it available to child classes.

  • Default

    No modifier is used, so access is limited to the same package. When no access modifier is specified, Java uses a default access level, often called package-private. This means the member is accessible only within classes in the same package. It is less restrictive than private but more restrictive than protected and public.

  • Public

    Accessible from any other class in the application, regardless of the package. The public access modifier is the least restrictive and specifies that the member can be accessed from any other class anywhere, whether within or in a different package. This access level is typically used for methods and variables that must be available consistently to all other classes.

Access modifiers also impact inheritance, determining which superclass members a subclass can inherit. For example, a subclass cannot inherit or override a private method from a superclass.

Q.No.2. what is the difference between Exception and error ?

ANS:

Exception:

*Exception is an event that disrupts the normal flow of a program's instructions. It's a signal that something unexpected or erroneous has occurred during the execution of the program.

*Its an unwanted or unexpected event, which occurs during the execution of a program, i.e. at run time, that disrupts the normal flow of the program’s instructions. Exceptions can be caught and handled by the program. When an exception occurs within a method, it creates an object. This object is called the exception object. It contains information about the exception, such as the name and description of the exception and the state of the program when the exception occurred.

ERROR:

* In Java, an error is a subclass of Throwable that tells that something serious problem is existing and a reasonable Java application should not try to catch that error. Generally, it has been noticed that most of the occurring errors are abnormal conditions and cannot be resolved by normal conditions. As these errors are abnormal conditions and should not occur, thus, error and its subclass are referred to as Unchecked Exceptions.

* In Java, we have the concept of errors as well as exceptions. Thus there exist several differences between an exception and an error. Errors cannot be solved by any handling techniques, whereas we can solve exceptions using some logic implementations. So, when an error occurs, it causes termination of the program as errors cannot try to catch.

Difference Between Exception and error:

In Java, errors and exceptions are both issues that can occur in a program, but they differ in a few ways.

Exceptions, on the other hand, are used to handle errors that can be recovered from within the program. Exceptions are represented by the Exception class and its subclasses. Errors are usually caused by serious problems that are outside the control of the program, such as running out of memory or a system crash. Errors are represented by the Error class and its subclasses, Whereas

We can recover from exceptions by either using try-catch block or throwing exceptions back to the caller.Recovering from Error is not possible.

Exceptions are defined in java.lang.Exception package. Whereas Error are defined in java.lang.Error package.

Causes

Exceptions are caused by the program's code, such as unexpected events like user input errors or file/network issues. Errors are caused by the system the program is running on, such as a lack of resources or hardware/software issues.

  • Occurrence

Exceptions can only occur at runtime. while Errors can occur at runtime or compile time.

  • Recoverability

    Exceptions can be caught and handled by the program using try-catch blocks to allow the program to continue running normally. Errors are unrecoverable and not meant to be caught by the program.

  • Checkability

    All errors are unchecked, but exceptions can be checked or unchecked.

Q.No.3. what is the difference between Checked Exception and Unchecked exception ?

ANS: In Java, Exception is an unwanted or unexpected event, which occurs during the execution of a program, i.e. at run time, that disrupts the normal flow of the program’s instructions.

In Java, there are two types of exceptions:

1.Checked exceptions

2.Unchecked exceptions

1. CheckedException:

*Java itself will know it might throw an exception and it allow you to handle it. Checked exceptions are caught during compile time.

*These are the exceptions that are checked at compile time. If some code within a method throws a checked exception, then the method must either handle the exception or it must specify the exception using the throws keyword. Checked exceptions can be fully checked or partially checked.

*Checkedexception can be created manually. This exception is counted as a sub-class of the class. Java Virtual Machine requires the exception to to be caught or handled.

*Checked exceptions happen when the source code is compiled into executable code. Checked exceptions require explicit handling in the code, using a try catch block or re-throwing.

1. IOException

* IoEcxeption is a checked exception that signals an error occurred during an input or output operation. It's a fundamental part of the java.io package and serves as the base class for many more specific I/O exceptions.

2. FileNotFoundException

*FileNotFoundException is another exception class available in the java.io package. The exception occurs when we try to access that file which is not available in the system. It is a checked exception because it occurs at run time, not compile-time, and it is thrown by one of the following constructors:

3. SqlException

* SQLException is a checked exception that occurs when there is an error accessing a database using JDBC. It provides information about the database error, including:

  • A message describing the error: This is a human-readable description of the error.

  • An SQLState string: This is a standardized code that identifies the type of error.

  • An error code: This is a vendor-specific code that provides more specific information about the error.

  • A chain to the next exception: This allows you to access additional exceptions that might have caused the error.

2. UnCheckedException:

*It will not make it compulsory to handle it. Unchecked exceptions occur during runtime.

*These are the exceptions that are not checked at compile time. In C++, all exceptions are unchecked, so it is not forced by the compiler’s to either handle or specify the exception. It is up to the programmers to be civilized and specify or catch the exceptions. In Java, exceptions under Error and RuntimeException classes are unchecked exceptions, everything else under throwable is checked.

*Unchecked exceptions occur when the program starts running. Unchecked exceptions don't require explicit handling. Unchecked Exceptions can also be created manually.

*This exception happens in runtime, and hence it is not included in the exception class. Java Virtual Machine does not need the exception to be caught or handled.

RunTimeException

1. ArithmeticException

*The arithmetic exception is a type of unusual outcome or unchecked error of the code, which is thrown or raised whenever a wrong mathematical or arithmetic operation appears in the code during run time. A runtime issue, also called an exception, appears whenever the denominator of a fraction is 0, and the JVM is not able to find out the result; hence the program execution is terminated, and an exception is raised. Note that at the point where the exception has been raised, the program terminates. However, the code earlier than that is executed, and the appropriate result is displayed.

2. ArrayIndexOutOfBoundException

*An ArrayIndexOutOfBoundsException in Java occurs when you try to access an element in an array with an index that is either:

  • Less than 0: Arrays are zero-indexed in Java, meaning the first element is at index 0.

  • Greater than or equal to the length of the array: The last element in an array is at index length - 1.

3. NullPointerException

*NullPointerException is raised in an application when we are trying to do some operation on null where an object is required. Some of the common reasons for NullPointerException in java programs are: Invoking a method on an object instance but at runtime the object is null.