Questions tagged [throws]

throws is a Java keyword. It is used in a method definition to declare the Exceptions to be thrown by the method.

Java distinguishes between checked and unchecked exceptions. Unchecked exceptions (RuntimeException, Error, and their subclasses) can be thrown without restriction. But if a method can generate a checked exception, the compiler requires that it either be caught (with a try/catch block) or declared in the method signature with the throws keyword.

https://en.wikibooks.org/wiki/Java_Programming/Keywords/throws https://docs.oracle.com/javase/tutorial/essential/exceptions/declaring.html

249 questions
-1
votes
2 answers

Why cant I use a method that throws in a catch block? Why use try/catch throw/throws in general?

This is the example. Taschenrechner just means calculator. This is the main class: Scanner sc = new Scanner(System.in); int num1 = sc.nextInt(); int num2 = sc.nextInt(); try { …
Fabian Andiel
  • 321
  • 1
  • 13
-1
votes
1 answer

When should we choose to throw an exception?

When should we chose to throws an exception? public Something sqlQuery(String sqlQuer) throws SqlException { } We can catch this exception in try catch. In which situation does we choose to use throws instead of catching immediately? is it some…
fatherazrael
  • 5,511
  • 16
  • 71
  • 155
-1
votes
1 answer

When throwing an exception from a java method, will a more general one override a more specific one?

The exact Error is: "There is a more general exception, 'java.lang.exception' in the throws list already" I have a method like so: public String myMethod() throws FileNotFoundException, IOException, Exception { try{ // DO…
mal
  • 3,022
  • 5
  • 32
  • 62
-1
votes
1 answer

compilation error with throwing exception

public class A { public void f(A a) { System.out.print("in A "); } } public class B extends A { public static void main(String[] args) { B b = new B(); A a = new A(); b.f(b); b.f(a); } } Why…
student
  • 25
  • 6
-1
votes
1 answer

Using exceptions and re-throwing

I am confused on how to do this step for my getAmount method This method should read the amount in one of the JTextFields and convert the field entry to a number. (I think i did this step correctly) Although a NumberFormatException will occur in…
HoodCoolege
  • 113
  • 7
-1
votes
2 answers

Covariant return type & throws declaration

In this below hierarchy, Below is the compiled code, for this question: class T3 {} class T2 extends T3{} class T1 extends T2{} class T5 extends T1{} class E3 extends Throwable {} class E2 extends E3 {} class E1 extends E2 {} class E5 extends E1…
overexchange
  • 15,768
  • 30
  • 152
  • 347
-1
votes
5 answers

How to use throws?

I am learning Java and come across the throws keyword. I came to know that it is used for exceptions we cant handle or don't wont to handle. Why can't we just use exception.printstacktrace? Or is there any situation where throws can perform auto…
Pavan Kumar
  • 77
  • 1
  • 6
-1
votes
5 answers

Java checked exceptions

I'm trying to understand checked exceptions in Java and have the following query. Is the following correct: If a method has the potential to throw a checked exception, of any type, that exception must either be declared using the throws keyword,…
danger mouse
  • 1,457
  • 1
  • 18
  • 31
-1
votes
5 answers

What is the difference between throw and throws Exception? And why does "throws" not require a catch/finally block?

I have a bit of confusion in the following snippet. try { public static void showMyName(String str) throws IOException { if(str.equals("What is your Name?")) throw new IOException; } }catch(IOException e) { …
Mitul Maheshwari
  • 2,647
  • 4
  • 24
  • 38
-1
votes
1 answer

Android, SQLite exception error with wrong inputs

Sorry, noob in Android. I have operations with SQLite database, working fine with correct inputs. Password work fine alltimes, but when i put wrong username. That means, insert when username is already exists or select when username dont exist, my…
Tomino
  • 113
  • 2
  • 14
-2
votes
1 answer

How do I read content from a .txt file and then find the average of said content in Java?

I was tasked with reading a data.txt file using the scanner import. The data.txt file looks like this: 1 2 3 4 5 6 7 8 9 Q I need to read and find the average of these numbers, but stop when I get to something that is not an integer. This is the…
juicecup
  • 1
  • 2
-2
votes
1 answer

Exception incompatible with throws clause

I am deveoloping small AOP standalone project there when i create my own exception it give this error exception incompatible with throws clause.please if any one can help appreciate very much. This is the exception class i was written package…
Siluni Upeksha
  • 419
  • 2
  • 6
  • 17
-2
votes
2 answers
-2
votes
1 answer

Method throws exception that java says isn't handled

When I try to run this code in my Main class my IDE pops up and says that I'm not handling the exception I'm throwing at the beginning of my method. public byte[] generateSalt() throws NoSuchAlgorithmException{ // VERY important to use…
Alex Day
  • 39
  • 1
  • 7
-2
votes
1 answer

How to create a for statement to handle my Exception

I had previously created a program that contained a customerList that added courses to each customer via their courseList. Now I have to modify the program (having a new list of customers) to throw an exception called CustomerNotEnrolledException if…
Aaron Gabriel
  • 37
  • 1
  • 1
  • 2
1 2 3
16
17