Questions tagged [throw]

throw is a keyword in various languages used for signaling an exception.

By throwing an exception the normal flow of control of a program gets interrupted. If this happens inside a try-block execution continues inside a matching catch or finally block, like in the following Java example:

try { 
    throw new RuntimeException("for demonstrating try-throw-catch");
    System.out.println("this doesn't get executed")
} catch (RuntimeException re) {
    System.out.println("flow of control goes directly here");
}

Use this tag for questions about the process or syntax of throwing exceptions.

Prefer for questions about the declaration of exceptions thrown by a method

Use or for questions about the catching side of the exception handling.

and for questions about the complete process of exception handling.

886 questions
-3
votes
1 answer

Prevent to crash bc. missing access rights

I have a folder, I like to be opened by NSWorkspace. But if access rights are missing, the entire app is crashing. How can I aviod this and show a message, that access rights were missing? I tried "try-catch", but this function does not throw…
Peter71
  • 2,180
  • 4
  • 20
  • 33
-3
votes
2 answers

Invalid conversion from throwing function of type XXXX to non-throwing function type XXXX

I am stuck with this situation where I have a custom JSONDecoder struct which contains a private function to decode data, and another function which is exposed, and should return a specific, Decodable type. I would like these functions to throw…
-3
votes
2 answers

StackTrace behavior in case of rethrow exception c#

As i already now, to keep the original stackTrace we should be avoiding using throw caught exception, instead, we should just use throw clause that in fact rethrow the exception. The thing is that a few days age in an interview i was given a code to…
Abolfazl
  • 1,592
  • 11
  • 32
-3
votes
2 answers

What happens to an exception that is thrown but not handled?

when i google what does throw do, the answer i get is that it throws an exception. wow. that means nothing to me at all. I have since learned thanks to stackoverflow that it tosses the error until the closest handler does something with it. There is…
-3
votes
5 answers

Unexpected 'undefined' output from 'throw' statement

I wrote a simple function to check if the passed parameter is 0, positive, or negative. I know for a fact that the passed parameter is a number and not any other data type. I am getting undefined as output while I expect the text provided in the…
-3
votes
1 answer

to create our own custom exception, throw or throws or try & catch?

in Java If we want to create our own custom exception, which keyword should we use? throw or throws or try & catch?
saboor
  • 23
  • 3
-3
votes
2 answers

Random Array: Getting min, max, and average

I am writing a class which is supposed to populate an array with random integers. The size of the array is given via user input via the scanner class. You are then supposed to include methods for obtaining the average, minimum, and maximum values,…
-3
votes
2 answers

Throw or return error

Let's say I have the following function: unsigned char *f(unsigned char*, int, int, long, const char*); I could change the return value to HRESULT (or my own defined), but I have to change the entire code inside a function. Or I could simly use…
Quest
  • 2,764
  • 1
  • 22
  • 44
-3
votes
1 answer

Can anyone please correct my given statement if its wrong(about throw/throws)

THROW:-We have to handle the exception(we in the sense user here). Throws:We are asking the compiler to handle the exception raised. Please correct if what I stated above is wrong . If wrong please tell me the correct statement. Thanks in Adv!
user3300851
  • 87
  • 1
  • 3
  • 12
-3
votes
3 answers

Is code between a throw() and catch() executed?

Okay, so I'm new to error handling, and I've seen some examples but I havent seen an answer for this question. I'll use some real basic example code to show what I'm asking. if(some condition){ throw Exception() } //Some random code in…
Eric Diviney
  • 327
  • 2
  • 5
  • 16
-4
votes
1 answer

How to debug try and catch blocks?

I was wondering if there is someone that can help me out with these two debugging assignments in Java, involving try/catch/throw statements. I can't seem to figure out how to debug either assignment working in NetBeans Zip file is attached. All or…
-4
votes
1 answer

Where to best place a throw in code

I'm writing some exception handling best practices based on several sources in the web. From the Microsoft webpage (https://msdn.microsoft.com/en-us/library/seyhszts(v=vs.110).aspx) I got the recommendation: "The stack trace begins at the statement…
-5
votes
1 answer

Exception in c++ not showing throw to cout

#include int main() { int num = 1; try { if (num != 0) { throw "num is not 0!"; } } catch (char *x) { cout << x << endl; } } I want this code to print "num is not 0!" to cout but when i run it i get…
Blink
  • 163
  • 1
  • 6
-5
votes
1 answer

The program has unexpectedly finished. try catch throw in Qt

I have a problem in my qt project, when an exception is thrown my windows crash and close, why? Where is the problem? I do not understand. class MyException:public std::exception{ private: QMessageBox* mex; public: …
Damien
  • 11
-5
votes
3 answers

Throwing exceptions syntax java

I'm having a hard time figuring out how to throw and catch exceptions. I Think I have the general syntax right, at least according to the book that I'm using, but I keep getting errors. Can anyone help me out with the syntax? public Movie( String…
user3420212
1 2 3
59
60