Exception Handling by Ayush
Exception Handling by Ayush
Catch: thid block catched all exceptiond that gere trapped in the try
block.
import java.io.*;
class GFG {
public static void main (String[] args) {
int a=5;
int b=0;
try{
System.out.println(a/b);
}
catch(ArithmeticException e){
e.printStackTrace();
}
}
}
Ques 4.Why do we need exception handling in
Java ?
Ans - Exception handling in Java is essential for several reasons:
1. Error Reporting: Exception handling allows developers to detect
and report errors or exceptional conditions that occur during
program execution. Without exception handling, errors might go
unnoticed, leading to unexpected behavior or program crashes.
2. Graceful Recovery: Exception handling enables programs to
gracefully recover from errors or exceptional situations. Instead of
abruptly terminating, programs can catch exceptions, perform
cleanup actions, and continue executing or take alternative paths.
3. Robustness: Exception handling contributes to the robustness of
Java programs by providing mechanisms to handle unexpected
conditions. By handling exceptions properly, developers can
prevent their programs from crashing and ensure smoother
execution even in the presence of errors.
4. Debugging: Exception handling aids in debugging and
troubleshooting programs. When an exception occurs, Java
provides detailed information about the exception type, stack
trace, and context, helping developers identify the root cause of
the problem.
5. Resource Management: Exception handling facilitates proper
management of system resources such as files, network
connections, and database connections. By catching exceptions
and releasing resources in the finally block, developers can ensure
that resources are properly cleaned up, even in the event of an
error.
6. Enhanced User Experience: Exception handling improves the user
experience of Java applications by providing informative error
messages or handling errors gracefully. Instead of presenting
users with cryptic error messages or crashing unexpectedly,
applications can handle errors elegantly and guide users on how
to proceed.
Ques 5.What is the different between exception
and error in Java ?
Ans -
Errors Exceptions
1. Checked Exceptions:
• Checked exceptions are exceptions that are checked by the
compiler at compile-time. This means that the compiler
ensures that these exceptions are either caught and handled
or declared in the method signature using the throws
keyword. Examples of checked exceptions include:
• IOException
• SQLException
• FileNotFoundException
• ClassNotFoundException
2. Unchecked Exceptions:
• Unchecked exceptions (also known as runtime exceptions)
are exceptions that do not need to be caught or declared
explicitly at compile-time. These exceptions typically occur
due to programming errors or exceptional conditions that
are beyond the control of the programmer. Examples of
unchecked exceptions include:
• NullPointerException
• ArrayIndexOutOfBoundsException
• ArithmeticException
• IllegalArgumentException
• ClassCastException
3. Errors:
• Errors represent abnormal conditions that occur in the Java
Virtual Machine (JVM) or in the underlying system. Unlike
exceptions, errors are typically not recoverable by the
application, and attempting to handle them may not be
practical. Examples of errors include:
• OutOfMemoryError
• StackOverflowError
• VirtualMachineError
• NoClassDefFoundError
can remove either finally block or catch block, but never both.