How to use try catch block in Java

+1 vote
I am writing a program for testing the nature of the number. I am using Eclipse and whenever there is an error the debugger section pops up and I am unable to verify the error. Hence, I want to use a try catch block to see what is the error I am getting. Can someone help?

Thank you!
Dec 21, 2018 in Java by Jino
• 5,820 points
2,044 views

2 answers to this question.

+1 vote

Hey @Jino,

Using try catch block for your code is always a good practice. Using try catch block you come to know about the different types of error and exceptions.

Here is the synatx:

try

{
     your block of code
}

catch(Exception e)// here if you know the type of exception going to happen you can mention that.

{
     System.out.println(e);
}
answered Dec 21, 2018 by Nabarupa
0 votes

Try block

The try block contains set of statements where an exception can occur. A try block is always followed by a catch block, which handles the exception that occurs in associated try block. A try block must be followed by catch blocks or finally block or both.

Syntax of try block

try{
   //statements that may cause an exception
}

While writing a program, if you think that certain statements in a program can throw a exception, enclosed them in try block and handle that exception.

Catch block

A catch block is where you handle the exceptions, this block must follow the try block. A single try block can have several catch blocks associated with it. You can catch different exceptions in different catch blocks. When an exception occurs in try block, the corresponding catch block that handles that particular exception executes. For example if an arithmetic exception occurs in try block then the statements enclosed in catch block for arithmetic exception executes.

Syntax of try catch in java

try
{
     //statements that may cause an exception
}
catch (exception(type) e(object))‏
{
     //error handling code
}
answered Aug 16, 2019 by Sirajul
• 59,230 points

Related Questions In Java

0 votes
2 answers

When and how to use Super() keyword in Java?

super() is used to call immediate parent. super() can be ...READ MORE

answered Jul 9, 2018 in Java by Sushmita
• 6,920 points
1,881 views
0 votes
1 answer

How to use pairs or 2-tuples in java?

You can refer the following code: public class ...READ MORE

answered Jun 7, 2018 in Java by Daisy
• 8,140 points
780 views
+1 vote
1 answer

How to tell the Maven to use the latest version of dependency in Java?

If you always want to use the ...READ MORE

answered Jun 26, 2018 in Java by Sushmita
• 6,920 points
15,431 views
+2 votes
1 answer

How to import an existing x509 certificate and private key in Java keystore to use in SSL?

keytool does not provide such basic functionality ...READ MORE

answered Dec 19, 2018 in Java by Daisy
• 8,140 points
2,610 views
0 votes
2 answers

How to ignore exception in Java?

There is no way to basically ignore a ...READ MORE

answered Aug 16, 2019 in Java by Sirajul
• 59,230 points
12,942 views
+1 vote
1 answer

Are arrays equivalent to objects in Java ?

Yes; the Java Language Specification writes: In the Java ...READ MORE

answered May 10, 2018 in Java by Rishabh
• 3,620 points
1,343 views
0 votes
2 answers

Connection reset : java.net.SocketException

You should check whether the client program is ...READ MORE

answered Sep 6, 2018 in Java by Sushmita
• 6,920 points
5,899 views
+1 vote
1 answer

Remove objects from an array in Java?

We can use external libraries: org.apache.commons.lang.ArrayUtils.remove(java.lang.Object[] array, int ...READ MORE

answered Jun 26, 2018 in Java by scarlett
• 1,290 points
1,204 views
0 votes
2 answers

How can I convert a String variable to a primitive int in Java

 Here are two ways illustrating this: Integer x ...READ MORE

answered Aug 20, 2019 in Java by Sirajul
• 59,230 points
2,354 views
+1 vote
2 answers

How to generate random integers within specific range in Java?

You can achieve that concisely in Java: Random ...READ MORE

answered Jul 25, 2018 in Java by samarth295
• 2,220 points
1,292 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP