exception Flashcards

1
Q

What is an exception?

A

An exception is an event, which occurs during the execution of a program, that disrupts the normal flow of the program’s instructions.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is error?

A

An Error indicates that a non-recoverable condition has occurred that should not be caught.

Error, a subclass of Throwable, is intended for drastic problems, such as OutOfMemoryError, which would be reported by the JVM itself.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Can we have the try block without catch block?

A

Yes, we can have the try block without catch block, but finally block should follow the try block.

Note:It is not valid to use a try clause without either a catch clause or a finally clause.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Which is superclass of Exception?

A

Throwable, the parent class of all exception related classes.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the difference throw and throws?

A

throws: Used in a method’s signature if a method is capable of causing an exception that it does not handle, so that callers of the method can guard themselves against that exception.

If a method is declared as throwing a particular class of exceptions, then any other method that calls it must either have a try-catch clause to handle that exception or must be declared to throw that exception (or its superclass) itself.

throw: Used to trigger an exception.

The exception will be caught by the nearest try-catch clause that can catch that type of exception. The flow of execution stops immediately after the throw statement; any subsequent statements are not executed.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What are the advantages of using exception handling?

A

Exception handling provides the following advantages over traditional error management techniques:

  • Separating Error Handling Code from Regular Code.
  • Propagating Errors Up the Call Stack.
  • Grouping Error Types
  • Error Differentiation.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How to create custom exceptions?

A

By extending the Exception class or one of its subclasses.

Example:

class MyException extends Exception { public MyException() { super(); } public MyException(String s) { super(s); } }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What are the types of Exceptions in Java

A

There are two types of exceptions in Java, unchecked exceptions and checked exceptions.

Checked exceptions: A checked exception is some subclass of Exception (or Exception itself), excluding class

RuntimeException and its subclasses. Each method must either handle all checked exceptions by supplying a catch clause or list each unhandled checked exception as a thrown exception.

Unchecked exceptions: All Exceptions that extend the RuntimeException class are unchecked exceptions.

Class Error and its subclasses also are unchecked.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What are the different ways to handle exceptions?

A

There are two ways to handle exceptions:

  • Wrapping the desired code in a try block followed by a catch block to catch the exceptions.
  • List the desired exceptions in the throws clause of the method and let the caller of the method handle those exceptions.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Why Errors are Not Checked?

A

A unchecked exception classes which are the error classes (Error and its subclasses) are exempted from compile-time checking because they can occur at many points in the program and recovery from them is difficult or impossible.

A program declaring such exceptions would be pointlessly.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Why Runtime Exceptions are Not Checked?

A

The runtime exception classes (RuntimeException and its subclasses) are exempted from compile-time checking because, in the judgment of the designers of the Java programming language, having to declare such exceptions would not aid significantly in establishing the correctness of programs.

Many of the operations and constructs of the Java programming language can result in runtime exceptions. The information available to a compiler, and the level of analysis the compiler performs, are usually not sufficient to establish that such run-time exceptions cannot occur, even though this may be obvious to the programmer.

Requiring such exception classes to be declared would simply be an irritation to programmers.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Explain the significance of try-catch blocks?

A

Whenever the exception occurs in Java, we need a way to tell the JVM what code to execute. To do this, we use the try and catch keywords.

The try is used to define a block of code in which exceptions may occur.

One or more catch clauses match a specific exception to a block of code that handles it.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is the use of finally block?

A

The finally block encloses code that is always executed at some point after the try block, whether an exception was thrown or not.

This is right place to close files, release your network sockets, connections, and perform any other cleanup your code requires.

Note:If the try block executes with no exceptions, the finally block is executed immediately after the try block completes. It there was an exception thrown, the finally block executes immediately after the proper catch block completes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What if there is a break or return statement in try block followed by finally block?

A

If there is a return statement in the try block, the finally block executes right after the return statement encountered, and before the return executes.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly