☕️JAVA Language Community
2.91K subscribers
144 photos
7 videos
31 files
42 links
☕️ Software, IT, Java, news
💻 IT highlights
🎯 AI update
🖥⌨️🖱
Download Telegram
#Java_Interview_Question

👉 #Exception_Handling in Java

The exception handling in java is one of the powerful mechanism to handle the runtime errors so that normal flow of the application can be maintained.

In this page, we will learn about java exception, its type and the difference between checked and unchecked exceptions.

👉What is exception

Dictionary Meaning: Exception is an abnormal condition.

In java, exception is an event that disrupts the normal flow of the program. It is an object which is thrown at runtime.

👉What is exception handling

Exception Handling is a mechanism to handle runtime errors such as ClassNotFound, IO, SQL, Remote etc.

👉Advantage of Exception Handling

The core advantage of exception handling is to maintain the normal flow of the application. Exception normally disrupts the normal flow of the application that is why we use exception handling.

👉Let's take a scenario:

statement 1;
statement 2;
statement 3;
statement 4;
statement 5;//exception occurs
statement 6;
statement 7;
statement 8;
statement 9;
statement 10;

Suppose there is 10 statements in your program and there occurs an exception at statement 5, rest of the code will not be executed i.e. statement 6 to 10 will not run. If we perform exception handling, rest of the statement will be executed. That is why we use exception handling in java.

@javaCode☕️
👍1
Hierarchy of Java Exception classes

@javaCode☕️
☕️JAVA Language Community
Hierarchy of Java Exception classes @javaCode☕️
#Java_Interview_Question

👉Types of Exception

There are mainly two types of exceptions:
checked and unchecked where error is considered as unchecked exception.

▪️The sun microsystem says there are three types of exceptions:

1️⃣Checked Exception

2️⃣Unchecked Exception

3️⃣Error

👉Difference between checked and unchecked exceptions

1) Checked Exception

The classes that extend Throwable class except RuntimeException and Error are known as checked exceptions e.g.IOException, SQLException etc. Checked exceptions are checked at compile-time.

2) Unchecked Exception

The classes that extend RuntimeException are known as unchecked exceptions e.g. ArithmeticException, NullPointerException, ArrayIndexOutOfBoundsException etc. Unchecked exceptions are not checked at compile-time rather they are checked at runtime.

3) Error

Error is irrecoverable e.g. OutOfMemoryError, VirtualMachineError, AssertionError etc.

@javaCode☕️
#Java_Interview_Question

👉Common scenarios where exceptions may occur

▪️There are given some scenarios where unchecked exceptions can occur. They are as follows:

1️⃣ArithmeticException

If we divide any number by zero, there occurs an ArithmeticException.

int a=50/0;
//ArithmeticException

2️⃣NullPointerException

If we have null value in any variable, performing any operation by the variable occurs an NullPointerException.

String s=null;
System.out.println(s.length());
//NullPointerException

3️⃣NumberFormatException

The wrong formatting of any value, may occur NumberFormatException. Suppose I have a string variable that have characters, converting this variable into digit will occur NumberFormatException.

String s="abc";
int i=Integer.parseInt(s);
//NumberFormatException

4️⃣ArrayIndexOutOfBoundsException

If you are inserting any value in the wrong index, it would result ArrayIndexOutOfBoundsException as shown below:

int a[]=new int[5];
a[10]=50;
//ArrayIndexOutOfBoundsException

@javaCode☕️
#Java_Interview_Question

73) What is Exception Handling?

Exception Handling is a mechanism to handle runtime errors .It is mainly used to handle checked exceptions.

@javaCode☕️
#Java_Interview_Question

74) What is difference between Checked Exception and Unchecked Exception?

1)Checked Exception

The classes that extend Throwable class except RuntimeException and Error are known as checked exceptions e.g.IOException,SQLException etc. Checked exceptions are checked at compile-time.

2)Unchecked Exception

The classes that extend RuntimeException are known as unchecked exceptions e.g. ArithmeticException,NullPointerException etc. Unchecked exceptions are not checked at compile-time.

@javaCode☕️
#Design_Patterns

#Creational_patterns

#Prototype_Design_Pattern

👉Intent

Specify the kinds of objects to create using a prototypical instance, and create new objects by copying this prototype.
Co-opt one instance of a class for use as a breeder of all future instances.
The new operator considered harmful.

@javaCode☕️
#Java_Interview_Question

75) What is the base class for Error and Exception?

Throwable.

@javaCode☕️
#Java_Interview_Question

76) Is it necessary that each try block must be followed by a catch block?

It is not necessary that each try block must be followed by a catch block. It should be followed by either a catch block OR a finally block. And whatever exceptions are likely to be thrown should be declared in the throws clause of the method.

@javaCode☕️
#Design_Patterns
#Prototype_Design_Pattern

👉Problem

Application "hard wires" the class of object to create in each "new" expression.

@javaCode☕️
#Design_Patterns
#Prototype_Design_Pattern

👉Discussion

Declare an abstract base class that specifies a pure virtual "clone" method, and, maintains a dictionary of all "cloneable" concrete derived classes. Any class that needs a "polymorphic constructor" capability: derives itself from the abstract base class, registers its prototypical instance, and implements the clone() operation.

The client then, instead of writing code that invokes the "new" operator on a hard-wired class name, calls a "clone" operation on the abstract base class, supplying a string or enumerated data type that designates the particular concrete derived class desired.

@javaCode☕️
#Prototype_Design_Pattern

👉Structure

The Factory knows how to find the correct Prototype, and each Product knows how to spawn new instances of itself.

@javaCode☕️
#Java_Interview_Question

77) What is finally block?

finally block is a block that is always executed.

@javaCode☕️
#Java_Interview_Question

78) Can finally block be used without catch?

Yes, by try block. finally must be followed by either try or catch.

@javaCode☕️
#Java_Interview_Question

79) Is there any case when finally will not be executed?

finally block will not be executed if program exits(either by calling System.exit() or by causing a fatal error that causes the process to abort).

@javaCode☕️
#Java_Interview_Question

80) What is difference between throw and throws?


1)throw is used to explicitly throw an exception.

throws is used to declare an exception.


2)checked exceptions can not be propagated with throw only.

checked exception can be propagated with throws.


3)throw is followed by an instance.

throws is followed by class.


4)throw is used within the method.

throws is used with the method signature.


5)You cannot throw multiple exception

@javaCode☕️
☕️JAVA Language Community
#Prototype_Design_Pattern @javaCode☕️
#Design_Patterns
#Prototype_Design_Pattern

👉Example

The Prototype pattern specifies the kind of objects to create using a prototypical instance. Prototypes of new products are often built prior to full production, but in this example, the prototype is passive and does not participate in copying itself. The mitotic division of a cell - resulting in two identical cells - is an example of a prototype that plays an active role in copying itself and thus, demonstrates the Prototype pattern. When a cell splits, two cells of identical genotype result. In other words, the cell clones itself.

@javaCode☕️
#Java_Interview_Question

81) Can an exception be rethrown?

Yes.

@javaCode☕️
#Java_Interview_Question

82) Can subclass overriding method declare an exception if parent class method doesn't throw an exception ?

Yes but only unchecked exception not checked.

@javaCode☕️