exception-handling

program to print 1-100 prime number and throw exception for composite number in given range

邮差的信 提交于 2019-12-06 15:04:27
问题 i have made a program to print 1-100 prime numbers. please help me to throw exception for composite number in range of 1-100 numbers. i am a beginner so any help will be appreciated. public static void main(String[] args) { System.out.println("Prime numbers from 1 - 100 are :"); int i = 0; int x = 0; for (i = 1; i <= 100; i++) { int ctr = 0; for (x = i; x >= 1; x--) { if (i % x == 0) { ctr = ctr + 1; } } if (ctr == 2) { System.out.println(i); } } } 回答1: I'd rather implement isPrime method and

Unhandled DivideByZero exception from an external DLL - C#

不羁的心 提交于 2019-12-06 14:18:11
I have a C# (.net 4.0) program, whose main is calling methods from an external FTP library - a dll the project references. The logic is in a try-catch block, and the catch prints the error. The exception handler has a generic parameter: catch(Exception ex) . The IDE is VS. Sometimes the FTP library throws the following division by zero exception. The problem is it is not caught in the catch block, and the program crashes. Exceptions originated in my wrapper code are caught. Anyone has any idea what the difference is and how the exception can be caught? The exception: Description: The process

PL/SQL Exception handling in procedures

喜欢而已 提交于 2019-12-06 14:11:28
问题 This is kind of a question about best practices. I have a PL/SQL block similar to this DECLARE --work variables PROCEDURE p1(in_parameter1, out_parameter1, out_parameter2...) IS BEGIN --do stuff --select ( ... ) into ( ... ) from t1 where ( ... ) END; PROCEDURE p2(in_parameter1, out_parameter1, out_parameter2...) IS BEGIN --do stuff --insert/update tables --do more stuff END; BEGIN -- MAIN PROCESS STARTS HERE open c1; fetch c1 into c1RowData; EXIT WHEN c1%NOTFOUND --call procedure1 --do stuff

Catch 401 error in Rails with Devise when user has multiple windows open

时光总嘲笑我的痴心妄想 提交于 2019-12-06 14:11:28
问题 Scenario is this: User has 2 windows on which s/he is logged in. S/he logs out of one, stays logged in on the other, and then on the latter, triggers some action, say a form submission. Initially, what happened is that it threw a invalid authenticity token error. I've updated the protect_from_forgery in ApplicationController to be with: :null_session so that instead of the Exception it throws a 401. Great! Now for step 2, rather than the user just seeing a line of text saying You need to sign

Raise an exception from the caller's scope? [duplicate]

我只是一个虾纸丫 提交于 2019-12-06 13:41:11
This question already has answers here : Python: eliminating stack traces into library code? (4 answers) Closed 6 years ago . Assume I have the following script: def do_not_call_on_one(i): if i == 1: raise ValueError("You never listen.") print "Success!" do_not_call_on_one(1) On excution, you will see the following traceback: Traceback (most recent call last): File "test.py", line 7, in <module> do_not_call_on_one(1) File "test.py", line 3, in do_not_call_on_one raise ValueError("You never listen.") ValueError: You never listen. Is there some way to manipulate the call stack, such that the

Error and exception handling in php7

孤人 提交于 2019-12-06 13:38:35
问题 Recently moved to php7. The following error occurs: argument 1 passed to MyClass\Throwable::exceptionHandler() must be an instance of Exception, instance of Error given And the respective class namespace MyClass; class Throwable { public function exceptionHandler(\Exception $exception) { //logic here } } As stated in docs most errors are now reported by throwing Error exceptions. Does it mean that I have to provide an instance of Error or even more general Throwable to the exception handler?

Exception handler for REST controller in spring

*爱你&永不变心* 提交于 2019-12-06 13:34:54
I want to handle exceptions so the URL information is automatically shown to the client. Is there an easy way to do this? <bean id="outboundExceptionAdapter" class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerExceptionResolver"> <!-- what property to set here? --> </bean> Ralph You have two choices: Spring Reference 15.9.1 HandlerExceptionResolver Spring HandlerExceptionResolvers ease the pain of unexpected exceptions that occur while your request is handled by a controller that matched the request. HandlerExceptionResolvers somewhat resemble the exception mappings

Spring return JSON for HTTP 406 (NOT_ACCEPTABLE)

故事扮演 提交于 2019-12-06 13:32:23
Spring allows the definition of @ExceptionHandlers inside of @RestControllerAdvice . I already defined a lot of other ExceptionHandlers in there for HTTP 400, 404, 405,... However the ExceptionHandler for HTTP 406 (NOT_ACCEPTABLE) does not seem to work. The handler is triggered, I checked that in the logs, but the result is not used. My goal is it to return a HTTP 406 with a JSON body. Variant 1 @ResponseStatus(HttpStatus.NOT_ACCEPTABLE) @ExceptionHandler(HttpMediaTypeNotAcceptableException.class) public ErrorDTO requestMethodNotSupported(final HttpMediaTypeNotAcceptableException e) { final

How did I crash in a @try/@catch block?

半城伤御伤魂 提交于 2019-12-06 13:29:41
I received the following crash report (truncated) Exception Type: EXC_CRASH (SIGSEGV) Exception Codes: 0x00000000, 0x00000000 Crashed Thread: 0 Thread 0 name: Dispatch queue: com.apple.main-thread Thread 0 Crashed: 0 libsystem_kernel.dylib 0x36df00d8 __psynch_mutexwait + 24 1 libsystem_c.dylib 0x31760674 pthread_mutex_lock + 376 2 CoreFoundation 0x31f2338a CFRunLoopWakeUp + 50 3 WebCore 0x308d0bc8 _WebThreadRun + 284 4 UIKit 0x37921c7a -[UIWebDocumentView _runLoadBlock:] + 38 5 UIKit 0x37921c3a -[UIWebDocumentView _cleanUpFrameStateAndLoad:] + 114 6 UIKit 0x37921bbe -[UIWebDocumentView

Exception handling: how granular would you go when it comes to argument validation?

旧街凉风 提交于 2019-12-06 12:20:20
问题 I'm coding a simple little class with a single method send an email. My goal is to implement it in a legacy Visual Basic 6 project, exposing it as a COM object via the COM Interop facility. There's a detail I'm finding difficult to resolve, that being how granular should I be at validating parameters. On that light, a thing I'm really not happy about, and which is not a detail at all, is the way I'm actually handling exceptions: public class MyMailerClass { #region Creation public void