Exception is an abnormal condition which occurs during the execution of a program and disrupts normal flow of the program. Exception can arise from different kind of situations such as network failure, illegal memory access, wrong input etc. The Exception handling provides a mechanism to handle compile and runtime errors. By handling the exceptions...

Read More

There are mainly two types of exceptions: checked and unchecked where error is considered as unchecked exception. 1.    Checked Exception 2.    Unchecked Exception 3.    Error

Read More

Checked Exceptions 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 throws keyword otherwise the compiler will complains that some checked exceptions are not handle properly in your code.Consider the f...

Read More

The classes that extend RuntimeException are known as unchecked exceptions. In Java exceptions under Error and RuntimeException classes are unchecked exceptions, everything else under Throwable is checked. Example of unchecked Exception are ArithmeticException, NullPointerException, ArrayIndexOutOfBoundsException etc. Unchecked Exceptions are norma...

Read More

“Throwable”, the parent class of all exception related classes and as we know the Object class is the parent of all classes in java and so is for Throwable. The following figure shows the Exception hierarchy.

Read More

An Error "indicates serious problems that a reasonable application should not try to catch." Errors are also unchecked exception & the programmer is not required to do anything with these. In fact it is a bad idea to use a try-catch clause for Errors. Most often, recovery from an Error is not possible & the program should be allowed to terminate. E...

Read More

   Checked Exceptions Unchecked Exceptions Checked Exceptions should be handled in the code using try-catch block or the method in which the code resides that can cause an checked exception should use throws keyword to let JRE know about these exception that might be thrown from the program. Unchecked Exceptions are not required to be handled in th...

Read More

There are different ways for Exceptions handling in Java. By using try, catch and finally blocks. In addition we can also use throw and throws keywords to handle exception. In the following section we will learn about these exception handling mechanism. try block : The code or group of statements which are to be monitored for exception are kept in ...

Read More

There are several combination of using try,catch and finally block for exception handling in Java. try{ //lines of code that may throw an exception }catch(Exception e){ //lines of code to handle the exception thrown in try block }finally{ //the clean code which is executed always no matter the exception occurs or not. } try{ //lines of code...

Read More

Throw keyword is used to throw the exception manually. It is mainly used when the program fails to satisfy the given condition and it wants to warn the application. The exception thrown should be subclass of Throwable. public class ExceptionTest { public static void main(String args[]) { int empId = 10; try { String userName = getUs...

Read More

If the function is not capable of handling the exception then it can ask the calling method to handle it by simply putting the throws clause at the function declaration. public class ExceptionTest { public static void main(String args[]){ int empId = 10; try{ String userName = getUserNameById(empId); System.out.println(userName); }catc...

Read More

Yes, we can throw multiple exceptions by throws keyword from a method. All the exceptions are declared in throws clause separated by comma. Order of exceptions declared in throws clause are not important. Those can appear in any sequence. Consider the following program where the method is throwing multiple exceptions. public class ExceptionTest { ...

Read More

When you are keeping multiple catch blocks, the order of catch blocks must be from most specific to most general ones. i.e sub classes of Exception must come first and super classes later. If you keep super classes first and sub classes later, compiler will show unreachable catch block error. public class ExceptionTest { public static void main...

Read More

Below are some important methods of Exception class : public String getMessage( ) : Returns a detailed message about the exception that has occurred. This message is initialized in the Throwable constructor. public Throwable getCause( ) : Returns the cause of the exception as represented by a Throwable object. public String toString( ) : Returns th...

Read More

No. We shouldn’t write any other statements in between try, catch and finally blocks. They form a one unit. try { // Statements to be monitored for exceptions } //You can't keep statements here catch(Exception ex) { //Cathcing the exceptions here } //You can't keep statements here finally { // finally block...

Read More

No, It shows compilation error. The try block must be followed by either catch or finally block. You can remove either catch block or finally block but not both. Following are the valid combinations. try{ //lines of code that may throw an exception }catch(Exception e){ //lines of code to handle the exception thrown in try block } try{ ...

Read More

Exceptions raised in the try block are handled in the catch block. If it is unable to handle that exception, it can re-throw that exception using throw keyword. It is called re-throwing an exception. try { Object object = null; /* The following statement will throw NullPointerException as object is Null and we are trying to exc...

Read More

This one is a classic interview question in core Java. final and finally are keyword, while finalize is method in Object class. final keyword is very useful for creating an Immutable class in Java By making a class final, we prevent it from being extended, similarly by making a method final, we prevent it from being overridden. On the other hand, f...

Read More

Exception handling in Java is polymorphic in nature. Forexample if you catch type Exception in your code then it can catch or throw itsdescendent types like FileNotFoundException aswell. So if you catch the type Exception before the type FileNotFoundException thenthe type Exception block will catch the entire exceptions and type FileNotFoundExcepti...

Read More

You should not try to catch the exception before your program can handle it in an appropriate manner. The natural tendency when a compiler complains about a checked exception is to catch it so that the compiler stops reporting errors. It is a bad practice to sweep the exceptions under the carpet by catching it and not doing anything with it. The be...

Read More

FOLLOW US ON LinkedIn



Explore Tutu'rself