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
0
votes
2 answers

Which Exceptions require a throws statement for the method?

In Java, there are some kinds of exceptions which require a throws statement: public void myMethod() throws IOException { throw new IOException("Error!"); } while others don't: public void myOtherMethod() { throw new…
gdiazc
  • 2,108
  • 4
  • 19
  • 30
0
votes
1 answer

Creating a Custom Exception

I'm trying to create a method f1(x) that throws an exception when x equals 5. After that I will try to call that method from another method f2() to invoke that exception. Then I have to have f2() recover by calling f1(x+1). I tried coding something,…
blutuu
  • 590
  • 1
  • 5
  • 20
0
votes
1 answer

Java reflection - Error throw

I have the following code: class ClassDetails { private String current_class; public ClassDetails(String current_class) { this.current_class = current_class; } public void getClassDetails() throws ClassNotFoundException { try { …
sticksu
  • 3,660
  • 3
  • 23
  • 41
0
votes
5 answers

Java writeout program Unhandled exception type IOException

I'm getting multiple erros with this part of my program. What it basically does is read input from an unorganized file, PersonnelInfo.txt, and it sorts it into an array in the EmployeeInfo class (not posted because I don't believe the problem lies…
Zach
  • 4,555
  • 9
  • 31
  • 52
0
votes
3 answers

I am ducking the exception and let it pass on to JVM, shouldn't I get the compile time error?

The sample code is:- import javax.sound.midi.*; import java.io.*; class test{ public void go() throws MidiUnavailableException{ //try{ Sequencer sequencer = MidiSystem.getSequencer(); System.out.println("Got it"); //} /*catch(Exception…
Ankit
  • 609
  • 2
  • 10
  • 26
0
votes
1 answer

How to throw error invalid url in android

I have created a gridview application and I get the path of an image with an url that is provided from json. JSON provide a lot of url paths of images, all urls has some invalid path. How can I throw error invalid url and go to get another…
kongkea
  • 9,788
  • 12
  • 39
  • 49
0
votes
3 answers

Factorial recursion Exception not given in java 1.6

Why I am not getting any Exception in the following code? After running this code I am getting an infinite loop mentioning at test.fact(t.java:32) No Compile-Time Error was found. class test { int fact(int m) throws Exception { if…
Rand Mate
  • 453
  • 3
  • 8
  • 14
0
votes
4 answers

error catching exception while System.out.print

I have 2 classes, one that implements a double lookup( int i); and one where I use that lookup(int i) in solving a question, or in this case printing the lookup values. This case is for an array. So I read the exception documentation or…
user1702633
  • 111
  • 3
  • 5
  • 8
0
votes
6 answers

Throwing Java Exceptions

When a method throws and exception, do we need to have a try block inside the method? For example, public void foo() throws SomeException{ try{ // content of method } } Is the try block required? Or, is the…
Deepank Vora
  • 67
  • 1
  • 6
0
votes
1 answer

Interface with exception extends interface without exception

Interface with Exception extends interface without exception Hay, I have a User-Table with the unique field email, which serves as username. Now when i call the dao.create method twice with the same information, i get an…
-1
votes
2 answers

Java Checked Exception

I'm struggling figuring out something about checked Exception in Java. What I've learned (I may be wrong) is that if you throw a checked Exception you have two options. you have to catch it with try catch block you delegate the caller with the…
-1
votes
2 answers

throw checked exception as unchecked in java instead of wrapping

I have the following code and i need to scan the Exception thrown. If it satisfies a particular condition i ignore the exception. Else i re-throw. The exception being thrown is a checked exception that means re-throwing is the hard part. Including…
D. Sikilai
  • 467
  • 1
  • 3
  • 17
-1
votes
1 answer

Throws in method signature of constructor

I have two constructors below and both executed just fine. Normally, Intellij will fuss at me if I didn't include throws in the method signature of the constructor, but in this particular instance, Intellij didn't bring up any errors. What would…
-1
votes
2 answers

Unable to subclass Runtime Exception

I want to "get around" the strict restrictions placed on methods by the throws clause. So I decided to subclass RuntimeException to make up a new, exempt exception of my own. Now I can throw it to my heart's content, because the throws clause that…
Anshul Gupta
  • 265
  • 2
  • 12
-1
votes
1 answer

Why doesn't Java support adding `throws` to the method signature?

This is a question that is being around in my head for days. When it comes to inheritance and polymorphism, Java support covariant return types through the bridge method concept. But why doesn't they implement a concept like bridge method to the…
Clarke
  • 185
  • 10