Java Basics 7

What are different types of exceptions?

In Java, there are two types of exceptions: checked exceptions and unchecked exceptions.

  • Checked exceptions are exceptions that are checked at compile-time. These are exceptions that extend the Exception class, but do not extend the RuntimeException class. Examples of checked exceptions include IOException and SQLException.
  • Unchecked exceptions, also known as runtime exceptions, are exceptions that are not checked at compile-time. These are exceptions that extend the RuntimeException class. Examples of unchecked exceptions include NullPointerException and ArithmeticException.

Additionally, there is also the Error class which are exceptions that are not intended to be caught by the application code, rather, they indicate serious problems that a reasonable application should not try to catch. Examples of errors include OutOfMemoryError and StackOverflowError.


Which is the parent class of all exceptions?

The parent class of all exceptions in Java is the Throwable class. The Throwable class is the superclass of all errors and exceptions in the Java language. It provides methods to print a stack trace and access the message that describes the exception. The Throwable class has two direct subclasses: Exception and Error. The Exception class is the superclass of all exceptions that can be caught and handled by application code, while the Error class is the superclass of all exceptions that indicate serious problems and should not be caught by application code.


Can we declare multiple exceptions?

Yes, it is possible to declare multiple exceptions in a single throws clause in a method signature in Java. When a method throws multiple exceptions, they should be separated by a comma.

Here's an example of a method that declares that it throws two different exceptions:Copy codepublic void myMethod() throws IOException, SQLException { // method implementation here}

In this example, the myMethod method declares that it throws both an IOException and an SQLException. This means that the code calling this method must either catch and handle these exceptions, or declare that it also throws these exceptions.


Can we declare multiple exceptions?

Yes, it is possible to declare multiple exceptions in a single throws clause in any order you like.

Here's an example of a method that declares that it throws two different exceptions in different order:

public void myMethod() throws IOException, SQLException {
  // method implementation here
}

n this example, the myMethod method declares that it throws both an IOException and an SQLException. This means that the code calling this method must either catch and handle these exceptions, or declare that it also throws these exceptions.


Can we declare Exception e first and Arithmetic after that in multiple exceptions?

If we place the generic exception catch block in the first place which means that none of the catch blocks placed after this block is reachable.

There will be Compile time error:

Exception in thread "main" java.lang.Error: Unresolved compilation problems: Unreachable catch block for ArithmeticException.

You should always place this block at the end of all other specific exception catch blocks.


When we do inheritance, exceptions also get inherited.

If a base class method throws an exception, that behavior will also occur in any derived classes that do not override the method. An overriding method may throw the same exception(s) that the base class method threw. An overriding method cannot add new exceptions to the throws list. Similar to placing more strict access on the method, this would restrict the derived class object in ways that a base class reference would be unaware of. If the derived class method does not throw the exception that the base class threw, it can either:

  1. Retain the exception in the throws list, even though it does not throw it; this would enable subclasses to throw the exception.
  2. Remove the exception from its throws list, thus blocking subsequent extensions from throwing that exception.

If you have a base class method that does not throw an exception, but you expect that subclasses might, you can declare the base class to throw that exception.


What is the difference between throw and throws?

In Java, the throw and throws keywords are used to handle exceptions, but they are used in different ways.

throw is used to explicitly throw an exception. It is used within a method or constructor to throw an exception. The syntax is as follows:

throw new ExceptionType(“exception message”);
//Ex
throw new IOException("Connection failed!!")

throws is used to declare that a method or constructor may throw an exception. It is used in the method or constructor signature. The syntax is as follows:

public void myMethod() throws ExceptionType {
  // method implementation here
}
//Ex
public int myMethod() throws IOException, ArithmeticException, NullPointerException {}

throw is used to throw an exception immediately and the execution of the current method will stop. The throws keyword is used to indicate that the method can throw an exception and the exception will be handled by the calling method.

If we see syntax wise then throw is followed by an instance variable and throws is followed by exception class names. The keyword throw is used inside method body to invoke an exception and throws clause is used in method declaration (signature). You cannot declare multiple exceptions with throw. You can declare multiple exception e.g. public void method()throws IOException,SQLException. Checked exceptions can not be propagated with throw only because it is explicitly used to throw an particular exception. checked exception can be propagated with throws.

In summary, throw is used to throw an exception and throws is used to declare that an exception may be thrown.


In java explain Exception propagation.

An exception propagates from method to method, up the call stack, until it's caught. So if a() calls b(), which calls c(), which calls d(), and if d() throws an exception, the exception will propagate from d to c to b to a, unless one of these methods catches the exception.


How to create your own exception?

n Java, creating your own exception class is simple and straightforward. You can create your own exception by creating a new class that extends either the Exception class or the RuntimeException class.

Here is an example of creating a custom exception class:

public class MyException extends Exception {
  public MyException() {
    super();
  }

  public MyException(String message) {
    super(message);
  }

  public MyException(String message, Throwable cause) {
    super(message, cause);
  }

  public MyException(Throwable cause) {
    super(cause);
  }
}


In this example, we created a new exception class called MyException and we extend the Exception class.

You can use your custom exception class just like any other exception class. You can throw an instance of it using the throw keyword and you can catch it using a catch block.

You can also create your custom exception class which extends RuntimeException class, and it's unchecked exception. The main difference between Checked and Unchecked exception is that, checked exception need to be handled and Unchecked exception doesn't need to be handled.

java exceptions

Creating a custom exception is particularly useful when you have a specific error case that you want to handle differently than the other exceptions, you can create your own exception class that inherits from the relevant exception class and add any additional information or behavior that you need.