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
vote
0 answers

How do I handle my user-specified exception in my test case

I previously wrote an entire successful program composed of 7 classes(Date, Address, Time, Customer, Course, InClassCourse, OnLineCourse), an interface (Invoice) and of course the test class (CustomerTest). The interface Invoice has a method…
1
vote
3 answers

Add throws signature in every method

I'm handling for the first time Exceptions in Java and I want to know if this this the good way to go. public static void main(String[] args) throws FileNotFoundException { submethod(); } static void submethod() throws…
fillobotto
  • 3,698
  • 5
  • 34
  • 58
1
vote
4 answers

Program for testing input and throwing exceptions how to restart when exception thrown JAVA

Essentially the idea of this program is to test user input and throw exceptions that I've created when invalid data is entered. For example: name cannot be empty and must be all alpha characters (no special or numeric). I have embedded this in a…
Exziled
  • 21
  • 4
1
vote
1 answer

No exception of type UnderflowException can be thrown;

I am trying to implement a stack with some functions in Java. I have created the class UnderflowException that implements Exception like this: package exceptions; public class UnderflowException extends Exception { public…
Stefan
  • 83
  • 1
  • 6
  • 20
1
vote
2 answers

How Do I Properly Use a "throws clause" in Java?

I'm still new to programming and I know that the code I have below is probably a mess but, how do I properly use a "throws clause"? I'm supposed to use it for an assignment(add a throws clause to the main method header) but when I try to run it, I…
1
vote
1 answer

Java: Exception Help Char and Int

Ok all, I'm stuck again with this code. I need to put in an Exception that won't allow the user to input 0 (because you can't divide later by 0) and the user cannot enter alpha characters. I am trying to display the message, disregard the wrong…
AnemonePoppy
  • 51
  • 1
  • 11
1
vote
2 answers

java - checked exception for 'throws' in overridden method

I was practicing exception handling mechanisms with method overriding in java...My code is as follows: class base { void show() { System.out.println("in base class method"); } } class derived extends base { void show()…
Krupal Shah
  • 8,949
  • 11
  • 57
  • 93
1
vote
0 answers

Throwing exceptions - performance java

Does throwing exceptions degrade the performance of an application? Is it possible to trace the performance [Resource utilization] to determine this? At any case, does an exception terminate the execution of the following statements? I want to know…
zumit
  • 189
  • 1
  • 9
1
vote
3 answers

"Method must return a result" when calling another method that only throws an exception

boolean method(int value) { switch(value) { case 0: return false; case 1: return true; default: Assert.fail("Unhandled value."); } } This fails compilation with the error "Method must return a result"…
Monstieur
  • 7,992
  • 10
  • 51
  • 77
1
vote
4 answers

What for the "throws" clause is used here and what if not used?

This is my code static void tryit() throws Exception { System.out.println(10 / 0); } public static void main(String[] args) { try { tryit(); } catch (Exception e) { System.out.println("Exception caught in main"); …
1
vote
2 answers

Calling method is not asking for to handle exception when i use throws

Here I have used throws in method signature when i am going to call this method from some where else that is not asking me to handle the exception. public class Exception { int a, b, c; void set(String data[]) throws…
1
vote
3 answers

call a variable from main method in another - java

public static void main(String[] args) throws Exception { URL oracle = new URL("http://www.example.com/example.php"); BufferedReader in = new BufferedReader(new InputStreamReader(oracle.openStream())); String inputLine; inputLine =…
user2835416
  • 11
  • 1
  • 2
1
vote
3 answers

Getting FileNotFoundException even though I declared it to be thrown

I am currently writing a Text Editor using linked lists, and I am pretty much done but I come across a FileNotFoundException when trying to test my program's command line, even though I declared it to be thrown. Here is the skeleton for my…
dmetal23
  • 141
  • 2
  • 3
  • 13
1
vote
1 answer

Rethrow different caught exceptions in java

I have the following dowload function. I catch some possible exceptions on the way, and store them in an Exception type variable, and after cleaning up in the finally block, I would like to re-throw the original exception (if there was one caught)…
Gavriel
  • 18,880
  • 12
  • 68
  • 105
1
vote
6 answers

Using throws java.io.IOException and getting System.in.read(); to access a case via an integer

I only need help with the input part. If the user inputs a number I need the program to read and output a case that equals the number that was input. //This program will display the months of the year public class MonthsOfTheYear { public…
YoungUser
  • 35
  • 1
  • 8