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
🌐
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
🌐
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 ...
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-try-catch-block
Java Try Catch Block - GeeksforGeeks
June 3, 2025 - Example: Here, we demonstrate the working try catch block with multiple catch statements. ... import java.util.*; class Geeks { public static void main(String[] args) { try { // ArithmeticException int res = 10 / 0; // NullPointerException String s = null; System.out.println(s.length()); } catch (ArithmeticException e) { System.out.println( "Caught ArithmeticException: " + e); } catch (NullPointerException e) { System.out.println( "Caught NullPointerException: " + e); } } }
🌐
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 inside the try-block, for instance from the divide method, the program flow of the calling method, callDivide, is interrupted just like the program flow inside divide. The program flow resumes at a catch-block in the call stack that can catch the thrown exception. In the example above the "System.out.println(result);" statement will not get executed if an exception is thrown fromt the divide method.
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.

🌐
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.
🌐
W3Schools
w3schools.com › java › java_try_catch.asp
Java Exceptions (Try...Catch)
The 'try catch' is finished. Try it Yourself » · The throw statement allows you to create a custom error. The throw statement is used together with an exception type. There are many exception types available in Java: ArithmeticException, FileNotFoundException, ArrayIndexOutOfBoundsException, SecurityException, etc:
Find elsewhere
🌐
Javatpoint
javatpoint.com › try-catch-block
Java Try-catch block - javatpoint
Java try-catch block. Let's see what is try and catch block and how can we write a simple program of exception handling
🌐
Oracle
docs.oracle.com › javase › tutorial › essential › exceptions › catch.html
The catch Blocks (The Java™ Tutorials > Essential Java Classes > Exceptions)
The following are two exception handlers for the writeList method: try { } catch (IndexOutOfBoundsException e) { System.err.println("IndexOutOfBoundsException: " + e.getMessage()); } catch (IOException e) { System.err.println("Caught IOException: " + e.getMessage()); }
🌐
Upgrad
upgrad.com › home › tutorials › software & tech › try catch in java
Try Catch in Java: Handling Exceptions with Grace
October 24, 2025 - The catch blocks are used to catch and handle specific types of exceptions that the code may throw within the try block. Multiple catch blocks can be chained together to handle different types of exceptions. java import java.util.Scanner; public class DivisionExample { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter the numerator: "); int numerator = scanner.nextInt(); System.out.print("Enter the denominator: "); int denominator = scanner.nextInt(); try { int result = numerator / denominator; System.out.println("Result: " result); } catch (ArithmeticException e) { System.out.println("Error: Division by zero is not allowed."); } scanner.close(); } }
🌐
Simplilearn
simplilearn.com › home › resources › software development › try catch in java
Try Catch in Java - Exception handling (With Examples) | Simplilearn
September 10, 2025 - Catch statements allow you to define a block of code that will be executed if an error occurs within a try block. Find out more about Try Catch in Java.
Address   5851 Legacy Circle, 6th Floor, Plano, TX 75024 United States
🌐
Coderanch
coderanch.com › t › 103854 › ide › Eclipse-catch-code-completion
Eclipse: try-catch code completion (IDEs and Version Control forum at Coderanch)
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums · this forum made possible by our volunteer staff, including ... ... I am using Eclipse 3.0.1. When I write coding, for example, I type "try", it will not give all code completion, it will not generate [b]automatically[b] whole try-catch template, and If-else will not, either.
🌐
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 ... 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 ....
🌐
CodeHS
codehs.com › tutorial › david › try-catch-in-java
Tutorial: Try Catch In Java | CodeHS
This tutorial covers the basics of try and catch statements in Java.
🌐
Guru99
guru99.com › home › java tutorials › exception handling in java
Exception Handling in Java
November 26, 2024 - Learn exception handling, try catch, exception hierarchy and finally block with examples in this tutorial.
🌐
BeginnersBook -
beginnersbook.com › home › java › try catch in java – exception handling
Try Catch in Java – Exception handling
May 30, 2024 - Try catch block is used for exception handling in Java. The code (or set of statements) that can throw an exception is placed inside try block and if the exception is raised, it is handled by the corresponding catch block. In this guide, we will see various examples to understand how to use ...
🌐
DataCamp
datacamp.com › doc › java › try
try Keyword in Java: Usage & Examples
In this example, the try block contains code that will throw an ArithmeticException due to division by zero. The catch block handles this exception and prints an appropriate message.
🌐
Software Testing Help
softwaretestinghelp.com › home › java › try, catch, finally and throw in java with examples
Try, Catch, Finally And Throw In Java With Examples
April 1, 2025 - In this tutorial, we will discuss the various keywords used in Java for Exception Handling such as Try, Catch, Finally, Throw and Throws with examples.