Exception handling in Java
Exception handling is a very important feature in Java. This
helps in handling bad user experience. Exceptions are run time errors.
Exception is a situation that stops the program while you are running. Just as
the amount of memory your program needs to execute, if your memory is not in
your computer, then execution of your program will stop. This is the out of
memory exception. Similar situations are called exceptions in java and they are
handled. Exception handing plays very important part in selenium. Exception handling interview questions will be based on below tutorials. Let's start with exception handing in details
Program Crashes
Just like if you forget to put semicolon in front of a
statement while creating a program, then the program error shows you and is not
compiled. Similarly, when exception is generated in java (or not handled), your
program stops and does not execute.
Handle a java program
If you want that your program does not stop executing on the
exception, then for this you have to handle exceptions. This is called
exception handling in java. When you handle exceptions, your program does not
stop at the exception, but instead executes the rest of the code by skipping
the exception code.
Java
Exception Handling
In Java, you handle exceptions with the help of some
keywords. All these keywords together form a structure that is very easy to
implement.
Try
The code of your program that can generate an exception, you
write it in the try block. For example, you are doing a mathematical operation
in the program and you feel that an exception can be generated, then you write
that code in the try block.
Throw
Most of the possible exceptions are already defined in the java library for you and these exceptions throw java automatically, you just have to handle them. But if you want, you can also create your own exceptions. You have to throw such exceptions yourself, for this you use throw keyword. You can also throw a predefined exception if you want.
Catch
If an exception is generated in the try block, it is handled
in this block. You write the code in this block that you want to execute when an
exception occurs. For example, you can print a message that tells the user that
an exception has been generated.
Throws
You can use nested try blocks. And in such a situation, if
you want an exception to handle the outer try block, then in such a situation
you use the throws keyword. You write the names of all exceptions by putting
the throws keyword next to the definition in the method. If an exception occurs,
the outer try block handles it.
Finally
After handling the exception, you finally write the code you
want to execute. After the exception in the try block, the compiler does not
execute that code and finally executes the block directly after the catch
block.
Steps to Handle Java Exceptions
First of all, write the code in the try block from which
exception can be generated.
After the try block, write the code to handle runtime exception
in java in the catch block. As such, you can print a message related to the exception.
After handling the exception, finally write the code that
you want to execute. It is very important for you to know about exceptions before
handling the Exceptions. There are some exceptions that occur regularly in
java. You can easily practice on these common exceptions and improve your
skill. Some common exceptions coming regularly in Java are being described
below.
Exceptions in Java
Arithmetic Exceptions
Any arithmetic error such as Arithmetic Exception is
generated if you try to divide a number by zero or try to store more value than
its size in a variable.
Class cast exception
Class-Cast-Exception is generated if you want to store the
reference of a class to another class and if the first class is not a subclass
of the second class.
Array Store Exception
If you have created a string array and you try to store
string in it, then Array-Store Exception is generated.
Array Index Out Of Bounds Exception
Array-Index-Out-Of-Bounds-Exception is generated if the size
of your array is 10 and you try to store the value at the 11th position or
access the 11th position.
Illegal Argument Exception
Illegal-Argument-Exception is generated when you pass an
invalid argument in a method such as passing the string instead of int.
Null Pointer Exception
In Java you can assign null value to a reference variable,
but if you try to use this reference variable, NullPointerException is
generated.
Number Format Exception
When you try to cast a string value to a number, a
Number-Format-Exception occurs.
We will bring more exception handling in java with examples. To know more about testing check this : Manual Testing
Exception handling is a very important feature in Java. This
helps in handling bad user experience. Exceptions are run time errors.
Exception is a situation that stops the program while you are running. Just as
the amount of memory your program needs to execute, if your memory is not in
your computer, then execution of your program will stop. This is the out of
memory exception. Similar situations are called exceptions in java and they are
handled. Exception handing plays very important part in selenium. Exception handling interview questions will be based on below tutorials. Let's start with exception handing in details
Program Crashes
Just like if you forget to put semicolon in front of a
statement while creating a program, then the program error shows you and is not
compiled. Similarly, when exception is generated in java (or not handled), your
program stops and does not execute.
Handle a java program
If you want that your program does not stop executing on the
exception, then for this you have to handle exceptions. This is called
exception handling in java. When you handle exceptions, your program does not
stop at the exception, but instead executes the rest of the code by skipping
the exception code.
Java Exception Handling
In Java, you handle exceptions with the help of some
keywords. All these keywords together form a structure that is very easy to
implement.
Try
The code of your program that can generate an exception, you
write it in the try block. For example, you are doing a mathematical operation
in the program and you feel that an exception can be generated, then you write
that code in the try block.
Throw
Most of the possible exceptions are already defined in the java library for you and these exceptions throw java automatically, you just have to handle them. But if you want, you can also create your own exceptions. You have to throw such exceptions yourself, for this you use throw keyword. You can also throw a predefined exception if you want.
Catch
If an exception is generated in the try block, it is handled
in this block. You write the code in this block that you want to execute when an
exception occurs. For example, you can print a message that tells the user that
an exception has been generated.
Throws
You can use nested try blocks. And in such a situation, if
you want an exception to handle the outer try block, then in such a situation
you use the throws keyword. You write the names of all exceptions by putting
the throws keyword next to the definition in the method. If an exception occurs,
the outer try block handles it.
Finally
After handling the exception, you finally write the code you
want to execute. After the exception in the try block, the compiler does not
execute that code and finally executes the block directly after the catch
block.
Steps to Handle Java Exceptions
First of all, write the code in the try block from which
exception can be generated.
After the try block, write the code to handle runtime exception
in java in the catch block. As such, you can print a message related to the exception.
After handling the exception, finally write the code that
you want to execute. It is very important for you to know about exceptions before
handling the Exceptions. There are some exceptions that occur regularly in
java. You can easily practice on these common exceptions and improve your
skill. Some common exceptions coming regularly in Java are being described
below.
Exceptions in Java
Arithmetic Exceptions
Any arithmetic error such as Arithmetic Exception is
generated if you try to divide a number by zero or try to store more value than
its size in a variable.
Class cast exception
Class-Cast-Exception is generated if you want to store the
reference of a class to another class and if the first class is not a subclass
of the second class.
Array Store Exception
If you have created a string array and you try to store
string in it, then Array-Store Exception is generated.
Array Index Out Of Bounds Exception
Array-Index-Out-Of-Bounds-Exception is generated if the size
of your array is 10 and you try to store the value at the 11th position or
access the 11th position.
Illegal Argument Exception
Illegal-Argument-Exception is generated when you pass an
invalid argument in a method such as passing the string instead of int.
Null Pointer Exception
In Java you can assign null value to a reference variable,
but if you try to use this reference variable, NullPointerException is
generated.
Number Format Exception
When you try to cast a string value to a number, a
Number-Format-Exception occurs.
We will bring more exception handling in java with examples. To know more about testing check this : Manual Testing
We will bring more exception handling in java with examples. To know more about testing check this : Manual Testing
0 comments: