Exception this words refers that something we should not do or something which is not allowed. Errors happen all the time in the software world. It might be an invalid user input or an external system that is not responding, or it’s a simple programming error. In all these situations, the errors occur at run time and the application needs to handle them. Otherwise, it crashes and can’t process further requests.
Getting an error, or exception, in your java program means the entire program will crash. You don’t want this to happen in real-world programs. Instead, you want the program to detect errors, handle them, and then continue to run. In that case you use exception management.
There are a couple of exceptions .They are (I included some of those which are commonly occurred and used.) :
You can read more from this link:
https://docs.oracle.com/javase/7/docs/api/java/lang/RuntimeException.html
Suppose you have a program where user input their phone number and password and if this valid they can enter the system. But unfortunately the user input different type, like in number they inputted string. So what will happen then?
Let’s see.
You will see some kind of this. Your program will not continue.
Solving run time errors with exception:
We handle exception in java by the means of two parts which known as try and catch.
Try block holds the part where the error actually occurs. In our case we need to put the inputted line of phone number into that try block.
Catch blocks holds which exception needs to be handled.
Let’s solve our problem with exception:
See this time there is no error we can complete our program by the help of exception class. We just put the input line of phone number into try block and in catch we pass the exception class object and then just print it.We got InputMismatchException here. Because we input string value into an integer value.
However, we can easily run a program with the help of exception class. And we should always kept in mind that exception itself a class and all those handled exceptions are into this super class.
Thanks for reading this blog.
Always hope for the best but prepare for the worst.
Comments