use of catch and try and block and this keyword in java icse
Answers
Answered by
2
HELLO THERE!!
The try{} and catch{} blocks are used in Java for Exception Handling.
Exception is an unexpected situation due to any errors that arise regarding Input/Output Operations.
Any error regarding I/O operation occuring in the try{} block is trapped. From the catch{} block, a message about the error is sent to the exception handler.
Consider the following example:
import java.util.*;
class Sample
{
public static void main (String args[])
{
try
{
Scanner sc = new Scanner (System.in);
int a, b;
System.out.println("Enter the two numbers: ");
a = sc.nextInt();
b = sc.nextInt();
int sum = a + b;
System.out.println("Sum is: " + sum);
}
catch (Exception e){}
}
}
THANKS!
The try{} and catch{} blocks are used in Java for Exception Handling.
Exception is an unexpected situation due to any errors that arise regarding Input/Output Operations.
Any error regarding I/O operation occuring in the try{} block is trapped. From the catch{} block, a message about the error is sent to the exception handler.
Consider the following example:
import java.util.*;
class Sample
{
public static void main (String args[])
{
try
{
Scanner sc = new Scanner (System.in);
int a, b;
System.out.println("Enter the two numbers: ");
a = sc.nextInt();
b = sc.nextInt();
int sum = a + b;
System.out.println("Sum is: " + sum);
}
catch (Exception e){}
}
}
THANKS!
Answered by
1
They are all exception handling blocks for unchecked exceptions.
Their main function is to handle the exceptions if encountered to prevent the program from terminating itself.
CATCH - it contains thecode or statements to be exeduted if an exception is encountered.
TRY - it contains the codes or statements where an exception may occur.
hope it helps
Thank u ★ ★ ★
#ckc
Their main function is to handle the exceptions if encountered to prevent the program from terminating itself.
CATCH - it contains thecode or statements to be exeduted if an exception is encountered.
TRY - it contains the codes or statements where an exception may occur.
hope it helps
Thank u ★ ★ ★
#ckc
Similar questions
Math,
7 months ago
Math,
7 months ago
India Languages,
7 months ago
Geography,
1 year ago
Math,
1 year ago