void indata() {
    model = cboModelo.getSelectedItem().toString();
    try {
        amount = Integer.parseInt(txtCantidad.getText());
    } catch (NumberFormatException e) {
        // Handle it here
    }
}
Answer from Dan Dosch on Stack Overflow
🌐
Coderanch
coderanch.com › t › 103854 › ide › Eclipse-catch-code-completion
Eclipse: try-catch code completion (IDEs and Version Control forum at Coderanch)
Second option, if you want a block of code to be surrounded with a try catch block, select the code that you want to be included in try-catch, right click brings up a popup menu source->surround with try-catch block. For more options, go to Window-->preferences-->java-->editor-->templates.
🌐
I'd Rather Be Writing
idratherbewriting.com › java-handling-exceptions
Java: Handling exceptions | I'd Rather Be Writing Blog and API doc course
January 2, 2015 - Run your more generic catch blocks last; this way your more specific catch blocks will identify the error more accurately. Eclipse example: handling_exceptions Eclipse example: handling_exceptions_2 Eclipse example: handling_exceptions3_compile_time Eclipse example: handling_exceptions4
🌐
projects.eclipse.org
ceylon-lang.org › documentation › 1.3 › reference › statement › try
try statement - Eclipse Ceylon
September 12, 2024 - The Ceylon Web IDE: allows you to try your Ceylon program in the browser, requires an OpenShift server that compiles the code and sends it back to the browser for execution. The Ceylon Eclipse IDE: fully-featured Ceylon editor in Eclipse · The Ceylon IntelliJ IDE: fully-featured Ceylon editor in IntelliJ, newer than the Eclipse plug-in and still a little bit behind in terms of features · The Java2Ceylon converter: Ceylon module that converts Java code in to Ceylon.
🌐
The Eclipse Foundation
eclipse.org › forums › index.php › t › 262620
Eclipse Community Forums: Java Development Tools (JDT) » Template for try/catch block | The Eclipse Foundation
The Eclipse Foundation - home to a global community, the Eclipse IDE, Jakarta EE and over 350 open source projects, including runtimes, tools and frameworks.
🌐
DZone
dzone.com › coding › frameworks › handling exceptions in java using eclipse
Handling Exceptions in Java Using Eclipse
June 4, 2010 - public class CodeName extends Exception { ……. } The compiler gives the programmer two choices when they call a method that throws an Exception that must be caught: 1. Add a try/catch in the code that is being call to catch the Exception 2. Pass ...
Find elsewhere
🌐
W3Schools
w3schools.com › java › java_try_catch.asp
Java Exceptions (Try...Catch)
Exception handling lets you catch and handle errors during runtime - so your program doesn't crash. ... The try statement allows you to define a block of code to be tested for errors while it is being executed.
🌐
Jenkov
jenkov.com › tutorials › java-exception-handling › basic-try-catch-finally.html
Basic try-catch-finally Exception Handling in Java
If an exception is thrown from ... the call stack to the method that called openFile(). If the calling method has a try-catch block, the exception will be caught there....
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-try-catch-block
Java Try Catch Block - GeeksforGeeks
June 3, 2025 - Example: Here, we are going to handle the ArithmeticException using a simple try-catch block. ... import java.io.*; class Geeks { public static void main(String[] args) { try { // This will throw an ArithmeticException int res = 10 / 0; } // Here we are Handling the exception catch (ArithmeticException e) { System.out.println("Exception caught: " + e); } // This line will executes weather an exception // occurs or not System.out.println("I will always execute"); } }
🌐
Oracle
docs.oracle.com › javase › tutorial › essential › exceptions › catch.html
The catch Blocks (The Java™ Tutorials > Essential Java Classes > Exceptions)
The catch block contains code that is executed if and when the exception handler is invoked. The runtime system invokes the exception handler when the handler is the first one in the call stack whose ExceptionType matches the type of the exception thrown. The system considers it a match if ...
Top answer
1 of 4
10

Explanation

From the Java documentation:

[The try block] contains one or more legal lines of code that could throw an exception. (The catch and finally blocks are explained in the next two subsections.)

An exception is a special kind of object. When you write new Exception(), you are creating a new exception object. When you write throw new Exception() you are creating a new error, and then throwing it to the nearest try-catch block, aborting the rest of your code.

When you throw an exception, it gets caught by the try-catch block that it's nested in (inside of). That is, assuming the proper catch block for that exception is registered. If the code is not wrapped in a try-catch block, the program with automatically shut down as soon as an error is thrown. Use a try-catch around any code or method that can throw an error, especially because of user input (within reason).

Some exceptions have to be caught, others are optional to catch. (checked vs. unchecked).

When you add throws to a method signature, you are announcing to other methods that if they call that method, it has the potential to throw a checked exception (it is not necessary for unchecked). Notice how it's throws not throw. It's not doing an action, it's describing that it sometimes does an action.

You use this functionality when you don't want to catch the error inside that method, but want to allow the method's that call your method to catch the error themselves.

Exceptions are a way to make your program respond coherently to unexpected or invalid situations and are especially useful when user input is required, though it's also useful in other situations such as File input/output.

Examples

public CircleWithException() throws InvalidRadiusException {
       this(1.0);
}

Here, the CircleWithException() has the potential to throw an InvalidRadiusException (presumably, the this(1.0) sometimes throws an InvalidRadiusException.)

The code calling this method should have:

try {
    new CircleWithException(); // This calls the method above
} catch (InvalidRadiusException e) { // The object "e" is the exception object that was thrown.
    // this is where you handle it if an error occurs
}

As I said before, an Exception is just a specific type of object that extends Exception

/* Invalid radius class that contains error code */
public class InvalidRadiusException extends Exception {
     private double radius;

/** Construct an exception */
public InvalidRadiusException(double radius) {
       super("Invalid radius " + radius);
       this.radius = radius;
}

/** Return the radius */
public double getRadius() {
    return radius;
 }
}

The above code defines a new type of Exception specific to your program/application. There are many predefined exceptions in the Java Standard Library, but often you need to create your own.

To throw this exception, you first create an InvalidRadiusException object and then throw it:

throw new InvalidRadiusException(1.0);
2 of 4
3

You can declare a method to throw an exception if you can't (or it's not convinient) to handle the exception inside the method.

In your case, you are calling the method setRadius inside the constructor. If you think that is convinient to handle the exception (that is thrown by setRadius) inside the constructor, you can use a try-catch clause:

public CircleWithException(double newRadius) throws InvalidRadiusException {
    try {
        setRadius(newRadius);
        numberOfObjects++;
    } catch (InvalidRadiusException e) {
        setRadius(0); // for example
    }
}

The catch block contains what you want to do if an exception were thrown. In this case, I'm setting the radius to 0, but you can change this.

Remember that it depends in your classes implementation and how you want them to work. If you don't want the constructor to handle this exception, you can throw it (as you are already doing) and handle it in other methods.

🌐
Programiz
programiz.com › java-programming › try-catch
Java try...catch (With Examples)
Here, we are trying to assign a value to the index 10. Hence, IndexOutOfBoundException occurs. ... The exception is thrown to the first catch block. The first catch block does not handle an IndexOutOfBoundsException, so it is passed to the next catch block. The second catch block in the above example is the appropriate exception handler because it handles an IndexOutOfBoundsException. Hence, it is executed. From Java SE 7 and later, we can now catch more than one type of exception with one catch block.
🌐
Oracle
docs.oracle.com › javase › tutorial › essential › exceptions › try.html
The try Block (The Java™ Tutorials > Essential Java Classes > Exceptions)
private List<Integer> list; private static final int SIZE = 10; public void writeList() { PrintWriter out = null; try { System.out.println("Entered try statement"); FileWriter f = new FileWriter("OutFile.txt"); out = new PrintWriter(f); for (int i = 0; i < SIZE; i++) { out.println("Value at: " + i + " = " + list.get(i)); } } catch and finally blocks .
🌐
CodeGym
codegym.cc › java blog › java exceptions › java try - catch
Java Try - Catch
February 20, 2025 - If no such block is found, an unhandled exception message is displayed to the user and further execution of the program is stopped. It is to prevent such an emergency stop that you need to use the try..catch block. Exception handling in Java is based on the use of the following keywords in a program: