0
how and what is the use of catch and try?
2 Answers
+ 3
Try and catch blocks are used to help handle exceptions errors that could disturb the normal flow of the program. So basically wherever you think an exception error could happen you need to put a try block around that code and a catch block that activates when an exceptions occurs within the try block. Quick example below:
System.out.println("enter a number");
double getNum = inputStreamName.nextDouble();
As you can see the example above expects the user to enter a double type number but a user could easily enter letter characters even though you've asked for a double type number and when the user doesnt enter the required info that is compatible with the expected data type then an exception error will pop up. So to prevent things like that we do this:
try{
System.out.println("enter a number");
double getNum = inputStreamName.nextDouble();
}catch(exception e){
System.out.println("please enter a premitive numeric number");
}
Finally this is just a quick intro to it but if you go to the oracle website, there will be alot of information and examples regarding it. Hopefully i've helped.
+ 1
thanks for entertaining my question..