Computer Science, asked by Prasanma, 1 year ago

use of catch and try and block and this keyword in java icse

Answers

Answered by Rajdeep11111
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!
Answered by chaitanyakrishn1
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
Similar questions