Java Exception and Error handling
what are the four access modifiers available in hava and what is their significance in term of class, method, and variable accessibility
In Java, there are four access modifiers that control the visibility and accessibility of classes, methods, and variables. These modifiers are:
public
protected
default (also known as package-private, which is the absence of an explicit modifier)
private
public
Classes: A class declared as
public
can be accessed from any other class in any package.Methods and Variables: Methods and variables declared as
public
can be accessed from any other class in any package.
protected
Classes: The
protected
modifier cannot be applied to classes (top-level classes).Inner Classes: Inner classes (nested classes) can be declared as
protected
.Methods and Variables: Methods and variables declared as
protected
can be accessed within the same package and by subclasses in different packages.default (package-private)
Classes: A class with no explicit access modifier is accessible only within the same package.
Methods and Variables: Methods and variables with no explicit access modifier are accessible only within the same package.
private
Classes: The
private
modifier cannot be applied to top-level classes.Inner Classes: Inner classes (nested classes) can be declared as
private
.Methods and Variables: Methods and variables declared as
private
can be accessed only within the same class in which they are declared.
- what is difference between exception and error
In Java, both
Exception
andError
are subclasses of theThrowable
class, but they represent different types of issues that can occur during the execution of a program. Here are the key differences betweenException
andError
:Exception
Definition:
- An
Exception
represents conditions that a reasonable application might want to catch and handle.
- An
Error
Definition:
- An
Error
represents serious problems that a reasonable application should not try to catch. These are typically external to the application and often represent catastrophic failures.
- An
Aspect | Exception | Error |
Definition | Conditions that an application might want to handle | Serious problems that applications should not try to catch |
Types | Checked (compile-time) and Unchecked (runtime) | Unchecked (runtime) |
Purpose | Handle anticipated and recoverable conditions | Indicate serious, often system-level issues |
Handling | Can be caught and handled | Not meant to be caught or handled |
Examples | IOException , SQLException , NullPointerException | OutOfMemoryError , StackOverflowError , VirtualMachineError |
- what is difference between checked exception and unchecked exception
In Java, exceptions are categorized into two main types: checked exceptions and unchecked exceptions. The difference between these two types lies primarily in how they are handled by the Java compiler and the programmer's responsibility in dealing with them.
Checked Exceptions
Definition:
- Checked exceptions are exceptions that are checked at compile-time. The compiler ensures that these exceptions are either caught or declared in the method signature using the
throws
keyword.
- Checked exceptions are exceptions that are checked at compile-time. The compiler ensures that these exceptions are either caught or declared in the method signature using the
Handling:
- The programmer is required to handle checked exceptions explicitly. This can be done using a try-catch block or by declaring the exception in the method's
throws
clause.
- The programmer is required to handle checked exceptions explicitly. This can be done using a try-catch block or by declaring the exception in the method's
Examples:
IOException
: Thrown when an input-output operation fails or is interrupted.SQLException
: Thrown when there is a database access error.ClassNotFoundException
: Thrown when an application tries to load a class through its string name but no definition for the class with the specified name could be found.
Usage:
- Checked exceptions are typically used for recoverable conditions where the program can take some action to recover from the exception.
Unchecked Exceptions
Definition:
- Unchecked exceptions are exceptions that are not checked at compile-time. These exceptions are subclasses of
RuntimeException
andError
.
- Unchecked exceptions are exceptions that are not checked at compile-time. These exceptions are subclasses of
Handling:
- The programmer is not required to handle unchecked exceptions explicitly. These exceptions can be caught, but it is not mandatory to declare them in the method signature.
Examples:
NullPointerException
: Thrown when an application attempts to usenull
in a case where an object is required.ArrayIndexOutOfBoundsException
: Thrown to indicate that an array has been accessed with an illegal index.ArithmeticException
: Thrown when an exceptional arithmetic condition has occurred, such as division by zero.
Usage:
- Unchecked exceptions typically indicate programming errors, such as logic errors or improper use of an API. These are generally conditions that the application cannot recover from or that are the result of a bug in the code.
- write a java program that reads user input for two integers and performs division. Handle the exception that is thrown when the second number is zero and display an error message to the user
-
- writ ethe code of arrayindexoutofboundsexeption&singleindexoutofboundsexception