An exception is an event, which occurs during the execution of a program, that interrupts the normal flow of the program. It is an error thrown by a class or method reporting an error in code.
The 'Throwable' class is the superclass of all errors and exceptions in the Java language.
Exceptions are broadly classified as
'checked exceptions' and
'unchecked exceptions'[RuntimeExceptions and Errors are unchecked exceptions]
Exceptions can be handled by using 'try-catch' block.
If a method doesn't handle the exception, then it is mandatory to specify the exception type in the method signature using 'throws' clause.
We can explicitly throw an exception using 'throw' clause.
The 'throws' clause in java programming language is belongs to a method to specify that the method raises particular type of exception while being executed.
The 'throws' clause takes arguments as a list of the objects of type 'Throwables' class.
Anybody calling a method with a throws clause is needed to be enclosed within the try catch blocks.
Use 'throw' statement to throw an exception or simply use the throw clause with an object reference to throw an exception.
The syntax is 'throw new Exception();'. Even you can pass the error message to the Exception constructor.
Multiple Cache Blocks
=====================
A single try block can have multiple catch blocks.
This is required when the try block has statements that generates different types of exceptions.
If the first catch block contains the Exception class object then the subsequent catch blocks are never executed.
The last catch block in multiple catch blocks must contain the Exception class object. This is because, the java complier gives an error saying that the subsequent catch blocks haven't been reached. This is known as Unreachable code problem.
Finally Block
==============
Finally block always executes immediately after try-catch block exits.
Finally block is executed incase even if an unexpected exception occurs.
We can’t have catch or finally clause without a try statement.
A try statement should have either catch block or finally block, it can have both blocks.
We can’t write any code between try-catch-finally block.
We can have multiple catch blocks with a single try statement.
try-catch blocks can be nested similar to if-else statements.
We can have only one finally block with a try-catch statement.
1) Exceptions are defined in which java package [java.lang.Exception]
2) What is Runtime Exception or unchecked exception?
[]
3) What is difference between throw and throws keyword in Java?
4) What is checked exception?
[Checked exception are the exceptions which forces the programmer to catch them explicitly in try-catch block. It is a subClass of Exception. ]
5) What is difference between Error and Exception?
6) What is difference between ClassNotFoundException and NoClassDefFoundError?
7) How to create custom Exception?
To create you own exception extend the Exception class or any of its subclasses.
Class New1Exception extends Exception { } // this will create Checked Exception
Class NewException extends IOExcpetion { } // this will create Checked exception
Class NewException extends NullPonterExcpetion { } // this will create UnChecked exception
8) Catch multiple Exceptions at once?
catch( Exception ex ) {
if ( ex is FormatException || ex is OverflowException || ex is ArgumentNullException ) {
return; }
throw; }
The 'Throwable' class is the superclass of all errors and exceptions in the Java language.
Exceptions are broadly classified as
'checked exceptions' and
'unchecked exceptions'[RuntimeExceptions and Errors are unchecked exceptions]
Exceptions can be handled by using 'try-catch' block.
If a method doesn't handle the exception, then it is mandatory to specify the exception type in the method signature using 'throws' clause.
We can explicitly throw an exception using 'throw' clause.
The 'throws' clause in java programming language is belongs to a method to specify that the method raises particular type of exception while being executed.
The 'throws' clause takes arguments as a list of the objects of type 'Throwables' class.
Anybody calling a method with a throws clause is needed to be enclosed within the try catch blocks.
Use 'throw' statement to throw an exception or simply use the throw clause with an object reference to throw an exception.
The syntax is 'throw new Exception();'. Even you can pass the error message to the Exception constructor.
Multiple Cache Blocks
=====================
A single try block can have multiple catch blocks.
This is required when the try block has statements that generates different types of exceptions.
If the first catch block contains the Exception class object then the subsequent catch blocks are never executed.
The last catch block in multiple catch blocks must contain the Exception class object. This is because, the java complier gives an error saying that the subsequent catch blocks haven't been reached. This is known as Unreachable code problem.
Finally Block
==============
Finally block always executes immediately after try-catch block exits.
Finally block is executed incase even if an unexpected exception occurs.
We can’t have catch or finally clause without a try statement.
A try statement should have either catch block or finally block, it can have both blocks.
We can’t write any code between try-catch-finally block.
We can have multiple catch blocks with a single try statement.
try-catch blocks can be nested similar to if-else statements.
We can have only one finally block with a try-catch statement.
1) Exceptions are defined in which java package [java.lang.Exception]
2) What is Runtime Exception or unchecked exception?
[]
3) What is difference between throw and throws keyword in Java?
4) What is checked exception?
[Checked exception are the exceptions which forces the programmer to catch them explicitly in try-catch block. It is a subClass of Exception. ]
5) What is difference between Error and Exception?
6) What is difference between ClassNotFoundException and NoClassDefFoundError?
7) How to create custom Exception?
To create you own exception extend the Exception class or any of its subclasses.
Class New1Exception extends Exception { } // this will create Checked Exception
Class NewException extends IOExcpetion { } // this will create Checked exception
Class NewException extends NullPonterExcpetion { } // this will create UnChecked exception
8) Catch multiple Exceptions at once?
catch( Exception ex ) {
if ( ex is FormatException || ex is OverflowException || ex is ArgumentNullException ) {
return; }
throw; }