Questions tagged [try-catch-finally]

A common usage of catch and finally together is to obtain and use resources in a try block, deal with exceptional circumstances in a catch block, and release the resources in the finally block.

435 questions
0
votes
1 answer

I can't use NumberFormatExeption. The program should continue even if the user input a string or non-number

Guessing the number import java.util.*; public class LabExer5A { public static void main(String args[]) { Scanner Input = new Scanner (System.in); System.out.println("Guess a number between 1 and 50"); int…
0
votes
2 answers

Understanding Try/Finally Suppressed Exceptions

I'm learning Exceptions and I found out that you can have suppressed ones. I read many examples on stackoverflow but still, they don't work as they are supposed to in the "try/finally" case: public class MultipleExceptionsExample { static class…
Unearthly
  • 121
  • 6
0
votes
1 answer

Why can't function A catch the error thrown by function B

I'm doing try and catch exercises in JavaScript. here is my code function getTime1() { return new Promise((resolve, reject) => { setTimeout(() => { resolve(11111); }, 1000); }); } function getTime2() { return new…
helloAl
  • 25
  • 4
0
votes
1 answer

Typescript Error: Variable 'resWithStatus' is used before being assigned. ts(2454)

for (const item of filesForUpload) { let resWithStatus: IResponseType; try { const res = await uploadFile(item); resWithStatus = { id: res?.id, name: res?.name, link: res?.link, ext: res?.ext, status:…
0
votes
0 answers

Why if we return from finally, rethrow from catch not works?

Why first case prints : "second level catch" "top level catch" and second case prints: only "second level catch"? Difference in finally block. Future main() async { try { secondLevelTryCatch(); } catch (e, s) { print("top level…
Андрей
  • 166
  • 2
  • 7
0
votes
1 answer

How do I exactly use try-catches for fault handling?

So currently I need to implement try-catches, but everywhere I look the try-catch can be relevant. How do you determine where to use the try-catches? Is there also a generalized way how to implement the try-catches? For example Is there some way how…
Jay
  • 189
  • 4
  • 12
0
votes
1 answer

How to return try block value in java even before finally block start executing?

I have code snippet in java like Int methodHitByAPI() { List returnValue = doSomething(); return returnValue; finishProcess(returnValue); } My doubt is that i wanted to execute doSomething() (this method was hit by a post api call…
Indra Neel
  • 125
  • 2
  • 7
0
votes
2 answers

How to ensure that a single finally block will be executed no matter what in a series of consecutive try...catch within the same method?

I want to execute a block of code even if ANY exception is caught in any of the preceding Try/Catch blocks, rather than having Finally blocks after every single Try/Catch. Is this possible to do? Example: public void Method() { try { } catch…
0
votes
0 answers

What is alternative to putting a lot of code inside one try catch block of java and not to use multiple try catch blocks

I have a lot of lines inside one try catch blocks . I want to make it such that even if any lines throw any error inside try block , try block should continue to run from next line onward. I hardcoded it with switch and case and whenever an error…
0
votes
2 answers

How to use finally block when using try-catch in a while loop

I have a usecase , wherein I need to read each line from a csv file : Each line has parameters for a function call. And I call the function for each entry. while(csvReader.readLine != null){ try { the line is divided into two parameters…
0
votes
0 answers

When will a Try and Catch statement break you out of Try?

If I have multiple Codelines within a Try statement. will it break me out as soon as 1 of them doesn't function?/through an error? and if yes, will it ignore the rest of the code within the try statement that is written after the error? Update: I…
0
votes
0 answers

Objective-C trycatch make method returns the method's caller

If a method defines a return, but trycatch works and catch a exception, I thought the method will return a nil, but actually it returns the caller. I am curious about what happened indeed in this operation. Here is the code: -…
childrenOurFuture
  • 1,889
  • 2
  • 14
  • 23
0
votes
1 answer

How does the compiler understand which exception to run below?

try { Image image = new Image("MyComputer/Documents/Photos/estambul.jpg"); try { //... image.print(); throw new NullPointerException(); } finally { …
Backwindow
  • 21
  • 4
0
votes
1 answer

How to use "tryCatch" to skip errors in a nested loop in R?

I am trying to load a few data with a nested-loop using pageviews. You already helped me get this result: library("pageviews") lang = c("it.wikipedia") bm = c("ECB","Christine Lagarde") x <- list( list(), list(), list(), list(), …
Rollo99
  • 1,601
  • 7
  • 15
0
votes
2 answers

tryCatch in R Programming

In R, I want to create a function that return Flag=0 if it encounters any error: Error<-function(x){ tryCatch(x, error=function(e{ Flag=0 } ) } When I enter: Error(5+b). It does not reflect Flag=0.
Ussu20
  • 129
  • 1
  • 12