0% found this document useful (0 votes)
18 views

Exception Handling

The document discusses exception handling in programming. It defines what exceptions are and why they occur. It then explains different ways to handle exceptions in Java code, including using try-catch blocks, finally blocks, and throwing and declaring exceptions with the throw and throws keywords.

Uploaded by

sarmadali4097
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Exception Handling

The document discusses exception handling in programming. It defines what exceptions are and why they occur. It then explains different ways to handle exceptions in Java code, including using try-catch blocks, finally blocks, and throwing and declaring exceptions with the throw and throws keywords.

Uploaded by

sarmadali4097
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Exception & Exception Handling:

Exception is something which occurred irregularly, Exception is basically something happened which
you’re not expecting. This word is used for all sorts of things that are not usually allowed. Real life
example of exception is you wake-up early on the morning and looking for your mobile charger but you
can’t find it anywhere it is an exception.

In programming exception can be occurred due to different situation, for example wrong user action
(invalid input etc.), trying to access the file which is not available in your disk or the path of the file is not
correct, network loss with server connection and etc. to make your software / application more reliable
we must have to handle the exception because if we don’t handle them the app will crash. Let suppose
we are trying to open a file which doesn’t exist on our memory so it will throw exception that
fileNotFound after throwing that exception our app will crash, but if we handle that exception, we can
show user a message that invalid path / file not found please select correct path, doing so our app will
not crash and user can select a desired file.

Another example of exception is user transferring data using our app and suddenly network connection
goes off, so our app will throw the exception but to avoid this we can print a message that no network
connection found and our app will wait until network connection connects back. Let see how exception
is thrown in runtime environment and it leads to program crash.

In above example we are trying to take an integer number from the user, but user enter invalid data like
it can be a string, character, float, double that’s why exception of InputMissMatchException this
exception break-down our application and we can’t do anything anymore to avoid this situation
exception handling comes into existence.
Exception Handling :
Exception handling is the process of responding to the occurrence of exceptions or we can say one of
the powerful procedures to handle the runtime errors so that the normal flow of the application can be
maintained. There are different ways to handle the exception in java which we will discuss one by one.

1. Try-Catch Block
Try catch block is used to handle the exception. The piece of code that can throw the exception is
written in the try block and exception which can be thrown by the code is catch in the catch block. Let
see an example to understand.

In above program we are taking an integer value from the user, but there is a chance that user enter
non-integer value which will cause InputMissMatchException to avoid this we have write down
sc.nextInt() in try block because it can through exception and we caught that exception in catch-block,
printed out the exception and again write down sc.nextInt() so that user can enter a valid integer value.
So, by using try-catch-block we not only handle the exception but our program is not crashed. Let see
another example when we try to access the file which is not available in our hard-drive or at given
location.
As shown in above example the FileNotFoundException is printed out, but exception is not thrown
because we handle that exception using try-catch block.

Finally Block:
Finally, block is used with try-catch block as well, finally block is used to define the piece of code which
will be executed at end no matter try-block / catch-block is executed or not. Finally, block will be
executed at the end no matter which one try / catch block is executed. Finally, block is used when you
have to do something at the end no matter what happened above. Let suppose your app runs only on
internet and if internet goes off it throws exception but we can catch that exception using catch block
but at the end no matter connection faced problem or not you want to disconnect the connection in
that case you can use finally block to disconnect the internet when app is closed. There are three cases
of try-catch block which are,

 try {} catch {}.


This will try a piece of code and check if an exception is thrown if yes then it can be handled in catch
block for example,
 try {} catch {} finally
This will try a piece of code if it throws an exception the exception will be handled in the catch block and
after that the finally block will be executed.
 try {} finally {}
This will try a piece of code if run successfully it will run the finally block but if the try block throws
exception in that case finally block will be executed and then the exception will be thrown. This
procedure is not a good technique because it can trash your application in case try block through
exception, but one thing to remember is that the finally block will be executed each and every time.

Even though the try block throws exception because there is no catch block to handle that exception but
the finally block is executed in that case as well and then the exception is thrown.

2. Throw – Throws keyword:


throw : throw keyword is used to declare the exception which is being thrown from the method or
block of code. This keyword throws given exception.

throws: this keyword is used to warn that the method of block of code can throw the exception, it is
used with method declaration / method signature. Let see some examples how throw and throws
keyword is used. Let see the user-defined method which will throw the exception.

1. Throw – Throws keyword:


throw : throw keyword is used to declare the exception which is being thrown from the method or
block of code. This keyword throws given exception.
throws: this keyword is used to warn that the method of block of code can throw the exception, it is
used with method declaration / method signature. Let see some examples how throw and throws
keyword is used. Let see the user-defined method which will throw the exception.
public static int getInfo ()throws InputMismatchException {
Scanner sc= new Scanner(System.in);
System.out.print("Enter an Integer Value : ");
if(!sc.hasNextInt()){
throw new InputMismatchException("The Given Data is Not A
Valid Entry");
}else return sc.nextInt();
}
In above example we have a method named getInfo() which is throwing InputMissMatchException it is
of integer type which means it will return an integer, inside that method we have a scanner class object
which takes an integer from the user and return it but if the scanner doesn’t contain integer value
(means user input non-integer value) in that case it will throw the InputMismatchException with
message “The Given Data is Not a Valid Entry”. Not let’s test this program.
public static void main(String[] args) {
try{
System.out.println(getInfo());
}catch (Exception E){
System.out.println(E);
}
}

Here we go we have created a main method and call the user-defined method in try-catch block because
it can throw the exception and inside catch block, we catch that exception and print out the exception as
seen in the below picture,

The user enters the wrong data so the exception is thrown by the method and that exception is handled
in the catch block and printed on console as shown in above example with custom message.

These are some of ways how we can handle the exception to make our application more reliable to
avoid any problem a user can occur at run-time.

------------------------------------------------------ End of Lecture ----------------------------------------------------------------

You might also like